Use of Android JSON and HttpGet

Source: Internet
Author: User

public class Jsonactivity extends activity {/** Called when the activity is first created. */Private TextView TV     Json;     Private Button Btnjson;     Private Button Btnjsonmulti;         @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);         Setcontentview (R.layout.main);         Tvjson = (TextView) This.findviewbyid (R.id.tvjson);         Btnjson = (Button) This.findviewbyid (R.id.btnjson);         Btnjsonmulti = (Button) This.findviewbyid (R.id.btnjsonmulti);                 Btnjson.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                 URL//String strURL = "Http://10.158.166.110:8080/AndroidServer/JsonServlet";                 String strURL = Serverpageutil.getstrurl (Urlsofserver.json_singer);                 Gets the JSON string returned by string strresult = Connserverforresult (strURL); Parse JSON string parseJson (strresult);         }         });  Btnjsonmulti.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v)                 {String strURL = Serverpageutil.getstrurl (urlsofserver.json_singers);                 String strresult = Connserverforresult (strURL);             Obtain multiple singer Parsejsonmulti (strresult);     }         }); } private String Connserverforresult (string strurl) {//HttpGet object HttpGet HttpRequest = new HttpGet (s         Trurl);         String strresult = "";             try {//HttpClient object HttpClient HttpClient = new Defaulthttpclient ();             Get HttpResponse object HttpResponse HttpResponse = Httpclient.execute (HttpRequest); if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Gets the returned data strresu             lt = entityutils.tostring (httpresponse.getentity ());    }     } catch (Clientprotocolexception e) {tvjson.settext ("protocol error");         E.printstacktrace ();             } catch (IOException e) {tvjson.settext ("IO error");         E.printstacktrace ();     } return strresult; }//Normal JSON data parsing private void Parsejson (String strresult) {try {jsonobject jsonobj = new JSON             Object (strresult). Getjsonobject ("singer");             int id = jsonobj.getint ("id");             String name = jsonobj.getstring ("name");             String gender = jsonobj.getstring ("gender");         Tvjson.settext ("ID number" +id + ", Name:" + name + ", Gender:" + gender);             } catch (Jsonexception e) {System.out.println ("Json parse Error");         E.printstacktrace (); }}//Parse multiple data json private void Parsejsonmulti (String strresult) {try {Jsonarray Jsonobjs             = new Jsonobject (strresult). Getjsonarray ("singers");          String s = "";   for (int i = 0; i < jsonobjs.length (); i++) {Jsonobject jsonobj = ((jsonobject) jsonobjs.opt (i))                 . Getjsonobject ("singer");                 int id = jsonobj.getint ("id");                 String name = jsonobj.getstring ("name");                 String gender = jsonobj.getstring ("gender");             s + = "ID number" +id + ", Name:" + name + ", Gender:" + gender+ "\ n";         } tvjson.settext (s);             } catch (Jsonexception e) {System.out.println ("Jsons parse Error!");         E.printstacktrace ();  }     } }

  

Use of Android JSON and HttpGet

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.