Obtain and parse JSON through network communication

Source: Internet
Author: User

We know that the display and update of information on mobile phones are from servers on the network. For an open interface like Sina, we obtain the data we want based on the interface specifications. In this case, we need to use the knowledge of network communication to obtain the string that he returns to us, we often get a string like JSON. We need to parse it to obtain the data bit we use.

Here we use the HttpClient network communication method to obtain the JSON string that exists in the server, parse the data, and use the adapter to adapt to the ListView control.

◆ Specific operations:

1) We set up a server, threw a json file in it, opened the server, and waited for the connection. Here, the JSON data file I used is

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>{"statuses": [{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false,"user":{"id":1222}},{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false,"user":{"id":1222}}] }


2) write a program to obtain the content in the JSON file on the server and read it as a String. Here I will provide the source code directly. If you do not understand it, refer to the previous blog)

// Instantiate the data request object httpGet = new HttpGet ("http: // 222.27.166.10: 8080/MyServer/jsonindex. jsp "); httpClient = new DefaultHttpClient (); // instantiate the client object httpResponse = httpClient.exe cute (httpGet); // instantiate the response object httpEntity = httpResponse. getEntity (); // get the data in the response and store in = httpEntity. getContent (); // get data content BufferedReader br = new BufferedReader (new InputStreamReader (in); // read stream String line = null; StringBuffer sb = new StringBu Ffer (); while (line = br. readLine ())! = Null) {sb. append (line); // the content in the sb is actually a JSON string}

3) Compile a method to parse the JSON string. This involves an array. Each JSON object can also be a collection of several JSON objects. We call this collection that carries the JSON object as a JSON array. array parsing uses JSONArray, get by the key name of a json object consisting of a JSON array. The details are as follows:

// JSON String Parsing. In the previous blog, I also explained public List <HashMap <String, Object> parseJSON (String str) {List <HashMap <String, object >>> list = new ArrayList <HashMap <String, Object >>(); HashMap <String, Object> map; try {JSONObject json = new JSONObject (str ); // obtain the JSON object angjsonarray js = (JSONArray) json based on the input string. get ("statuses"); // obtain the JSON array for (int I = 0; I <js. length (); I ++) {// cyclically array read Data map = new HashMap <String, Object> (); JSONObject jo = (JSONObject) js. get (I); // each array is a JSON object map. put ("calendar_id", jo. getString ("calendar_id"); // data storage map. put ("title", jo. getString ("title"); map. put ("category_name", jo. getString ("category_name"); map. put ("showtime", jo. getString ("showtime"); map. put ("endshowtime", jo. getString ("endshowtime"); map. put ("allDay", jo. getBoolean ("allDay"); JSONObject joo = jo. getJSONObject ("user"); // a json object map is nested here. put ("id", joo. getString ("id"); list. add (map); // add data to the List} catch (JSONException e) {e. printStackTrace ();} return list; // return the list of stored data}

4) declare and instantiate the ListView and adapter to complete adaptation. The code here should be written directly after the second operation)

// Assign values to the data source and forcibly convert list = (ArrayList <HashMap <String, Object>) parseJSON (sb. toString (); // instantiate the adapter to store data // the data in the new String here is the key name in JSON, the ID of the control in the custom layout in the new int. // because the JSON data here has been obtained, the output shows that each JSON object contains a total of seven data records, so here we use 7 // controls to carry data adpater = new SimpleAdapter (this, list, R. layout. cell, new String [] {"calendar_id", "title", "category_name", "showtime", "endshowtime", "allDay", "id "}, new int [] {R. id. textView1, R. id. textView2, R. id. textView3, R. id. textView4, R. id. textView5, R. id. textView6, R. id. textView7}); // adapted to Listview lv. setAdapter (adpater );

◆ Result: the Listview control filled with data is displayed on the simulator. The data in the control is parsed. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/101452E45-0.jpg "title =" Unnamed .bmp "/>

Network Acquisition and parsing of JSON strings is very important to us and is also a must .. Come on .. 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1014523310-1.gif "/>



This article is from the "Schindler" blog, please be sure to keep this source http://cinderella7.blog.51cto.com/7607653/1292283

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.