[Android] HttpPost The POST request to pass JSON data

Source: Internet
Author: User

Lazy Typing

/*** Send a POST request to pass JSON*/     Public voidJiexi () {NewThread (NewRunnable () { Public voidrun () {//quotation marks in JSON must be added \ escapedString Getlightjson = "{\" object\ ": \" light\ ", \" action\ ": \" Get\ "}"; String jsonparent=getjsonparent (Getlightjson); if(Ifjsonparentok (jsonparent)) {ArrayList<light> query =getquery (jsonparent);  for(Light l:query) {System.out.println ("----------"); System.out.println ("Co2-->" +L.getco2 ()); System.out.println ("Pm2.5-->" +l.getpm2_5 ()); System.out.println ("----------");    }}}). Start (); }    /*** Return JSON * *@paramJSON *@return     */     Publicstring Getjsonparent (String json) {Try{URI Uri=NewURI (urlstring); HttpPost Mhttppost=NewHttpPost (URI); //set the request headerMhttppost.setheader ("Accept", "Application/json"); Mhttppost.setheader ("Content-type", "Application/json"); //JSON data is hereHttpentity mentity =Newstringentity (JSON, HTTP.            UTF_8);            Mhttppost.setentity (mentity); //Send RequestHttpResponse response =Newdefaulthttpclient (). Execute (mhttppost); String Str=entityutils.tostring (Response.getentity ()); System.out.println ("--->" +str); returnstr; } Catch(Exception e) {}return""; }    /*** Return code is NOT OK * *@paramSTR *@return     */     Public BooleanIfjsonparentok (String str) {Try{jsonobject=Newjsonobject (str); String result= jsonobject.getstring ("Result"); if(Result.equals ("OK")) {                return true; }        } Catch(jsonexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return false; }    /*** Parse the returned JSON * *@paramSTR *@return     */     PublicArraylist<light>getquery (String str) {ArrayList<light> arrayList =NewArraylist<light>(); Try{Light _light=NewLight (); Jsonobject=Newjsonobject (str); _light.setpm2_5 (Jsonobject.getstring ("PM2.5")); _light.setco2 (Jsonobject.getstring ("CO2"));            Arraylist.add (_light); returnarrayList; } Catch(jsonexception e) {e.printstacktrace (); }        return NULL; }

[Android] HttpPost The POST request to pass JSON data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.