Android requests JSON data via httpclient and resolves

Source: Internet
Author: User

Create a ashx file with. NET and Response.Write the JSON format

public void ProcessRequest (HttpContext context)        {            context. Response.ContentType = "Text/plain";            StringBuilder  Jsonbuilder  = new StringBuilder ();            Jsonbuilder.append ("{\" parent\ ":");            Jsonbuilder.append ("{\" children\ ":");            Jsonbuilder.append ("[");            Jsonbuilder.append ("{\" id\ ": \" 11111\ ", \" title\ ": \" aaaaaaa\ ", \" name\ ": \" 111111111aaaaaaaaaa\ "},");            Jsonbuilder.append ("{\" id\ ": \" 22222\ ", \" title\ ": \" bbbbbbb\ ", \" name\ ": \" 222222222aaaaaaaaaa\ "},");            Jsonbuilder.append ("{\" id\ ": \" 33333\ ", \" title\ ": \" ccccccc\ ", \" name\ ": \" 33333333aaaaaaaaaa\ "}");            Jsonbuilder.append ("]}}");             Context. Response.Write (Jsonbuilder.tostring ());        }

Android Code

public class Httpclient_get_activity extends Activity {public TextView mytextview = null; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method stub Super.oncrea      Te (savedinstancestate);           This.setcontentview (r.layout.httpget_httpclient_activity);      Button btn = (button) This.findviewbyid (R.id.button1);              Btn.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View v) {         New Readhttpget (). Execute ("Http://190.160.10.79:7890/handler1.ashx");   }              }); } class Readhttpget extends Asynctask<object, object, object> {@Override protected object Doinback Ground (Object ... params) {//TODO auto-generated method stub HttpGet HttpRequest = new HttpGet (PA         Rams[0].tostring ());            try {HttpClient HttpClient = new Defaulthttpclient (); HttpResponse HttpResponse = Httpclient.execute(HttpRequest); if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {String strresult = Entity                Utils.tostring (Httpresponse.getentity ());            return strresult;            } else {return "request Error";            }} catch (Clientprotocolexception e) {} catch (IOException e) {         TODO auto-generated Catch block E.printstacktrace ();      } return null; } @Override protected void oncancelled (Object result) {//TODO auto-generated method stub Supe      r.oncancelled (result); } @Override protected void OnPostExecute (Object result) {//TODO auto-generated method stub su                 Per.onpostexecute (result); try {//Create a JSON object Jsonobject jsonobject = new Jsonobject (result.tostring ()). Getjsonobject (" Parent ");                       Gets the JSON array for an object Jsonarray Jsonarray = Jsonobject.getjsonarray ("Children");                       StringBuilder builder = new StringBuilder (); for (int i = 0; I<jsonarray.length (); i++) {//Create a new JSON object that is one of the object JSON in an array                               Object jsonObject2 = (jsonobject) jsonarray.opt (i); Builder.append (jsonobject2.getstring ("id"));                Get Data Builder.append (jsonobject2.getstring ("title"));            Builder.append (jsonobject2.getstring ("name"));         } mytextview.settext (Builder.tostring ());         } catch (Jsonexception e) {//TODO auto-generated catch block E.printstacktrace ();         }} @Override protected void OnPreExecute () {//TODO auto-generated method stub         Super.onpreexecute ();   Toast.maketext (Getapplicationcontext (), "Start HTTP GET request", Toast.length_long). Show ();   } @Override protected void Onprogressupdate (Object ... values) {//TODO auto-generated method stub      Super.onprogressupdate (values); }          }}

The shallow understanding of JSON in Android

Jsonobject representation {"Key": "Value"}

Jsonarray representation [{"Key": "Value"},{"key": "Value"},{"key": "Value"}],jsonarray contains multiple Jsonobject

Access via Jsonobject object, use Jsonobject.getxxx ("key") to get the corresponding value

The general parsing JSON uses these two.

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.