Android reads JSON Array

Source: Internet
Author: User

Some time ago, I wrote a code to read the webpage and return the JSON array. Here, I will sort it out for future rapid development of similar programs.

Define a data object class:

package com.example.jsonreadtest;public class MessageItemData {public String mMessageUrl = null;public String mMessageTitle = null;public String mMessageIndex = null;public String mMessageBitmapLocalPosition = null;}

Write this method in a separate class to facilitate the use of each place in the program, the idea of this function is to get the result returned by the webpage and convert it to jsonarray (it took a long time to get the result from this place before, because I don't know whether to convert the result into a jsonarray or a jsonobject), traverse the entire array cyclically, and store the object data information in messageitemdata.

Package COM. example. jsonreadtest; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. unsupportedencodingexception; import Java. util. arraylist; import Java. util. list; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. namevaluepair; import Org. apache. HTTP. client. clientprotocolexcepti On; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import Org. JSON. jsonarray; import Org. JSON. jsonexception; import Org. JSON. jsonobject; import android. util. log; public class datasource {public static St Ring imageurl = "imageurl"; public static string messagetitle = "messagetitle"; public static string messageindex = "messageindex"; public static string messagecontent = "messagecontent "; public static string mainurl = "http: // 120.11.181.104/android. PHP? Act = three "; public static string columnurl =" http: // 120.11.181.104/Android. php? Act = two "; public static string subjecturl =" http: // 120.11.181.104/Android. php? Act = one "; public static void getjsonarraycontent (string purl) {list <messageitemdata> messageitemdatalist = new arraylist <messageitemdata> (); /* store the HTTP request result */string result = ""; string Ss = NULL; jsonobject jobject = NULL; /* data packets to be sent */arraylist <namevaluepair> namevaluepairs = new arraylist <namevaluepair> (); namevaluepairs. add (New basicnamevaluepair ("year", "1980"); inputstream is = NULL; // HTTP POST /* Create an httpclient object */httpclient = new defaulthttpclient ();/* Create an httppost object */httppost = new httppost (purl ); /* set the request data */try {httppost. setentity (New evaluate (namevaluepairs);} catch (unsupportedencodingexception E1) {// todo auto-generated catch blocke1.printstacktrace ();}/* Create httpresponse object */httpresponse response = NULL; try {response = httpclient.exe cute (HTT Ppost);} catch (clientprotocolexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} catch (ioexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} /* obtain the Response Message entity */httpentity entity = response. getentity ();/* create a data stream pointing to an object */try {is = entity. getcontent ();} catch (illegalstateexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} catch (io1_tio N E1) {// todo auto-generated catch blocke1.printstacktrace ();} Try {bufferedreader reader = new bufferedreader (New inputstreamreader (is, "iso-8859-1"), 8 ); stringbuilder sb = new stringbuilder (); string line = NULL; while (line = reader. readline ())! = NULL) {sb. append (LINE + "/N");} is. close (); Result = sb. tostring (); jsonarray jarray; try {jarray = new jsonarray (result); For (INT I = 0; I <jarray. length (); I ++) {messageitemdata = new messageitemdata (); jsonobject json_data = jarray. getjsonobject (I); messageitemdata. mmessageurl = json_data.getstring (imageurl); messageitemdata. mmessageindex = json_data.getstring (messageindex); messageitemdata. mmessagetitle = json_data.getstring (messagetitle); messageitemdatalist. add (messageitemdata) ;}} catch (jsonexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} catch (exception e) {}// parse JSON data }}

Main program call:

public class MainActivity extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        /*connecting();*/        new ReadTextMessageThread().start();    }        class ReadTextMessageThread extends Thread {public void run() {DataSource.getJsonArrayContent(DataSource.MAINURL);}}    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}

 

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.