Android uses JSON for Network Data Transmission

Source: Internet
Author: User

Network data transmission and resolution are used. Here, the JSON method is used. Compared with the traditional XML parsing method, this method has some advantages. First, it is more lightweight than XML, and writing an XML file is annoying, while JSON is relatively easy.

The Android platform has jsong-related classes for JSON data parsing. The tragedy is that they are available only after Android sdk3.0. However, on Google's website: The 3.0 platform, this part is actually directly integrated into Android. We want to parse JSON data, download a jar package directly on the website, and import it to the project to parse JSON data.

The following example clearly explains how to work:

Let's take a look at two self-encapsulated classes:

Httputils. Java:

Public class httputils {// download the JSON data from the server, that is, a string

Public static string getdata (string URL) throws exception {

Stringbuilder sb = new stringbuilder ();

Httpclient = new defaulthttpclient ();

Httpget = new httpget (URL );

Httpresponse = httpclient.exe cute (httpget );

Httpentity = httpresponse. getentity ();

If (httpentity! = NULL ){

Inputstream instream = httpentity. getcontent ();

Bufferedreader reader = new bufferedreader (New inputstreamreader (

Instream ));

String line = NULL;

While (line = reader. Readline ())! = NULL ){

SB. append (line );

}

Return sb. tostring ();

}

Return NULL;

}

 

 

Jsonutils. Java:

 

Public class jsonutils {

Public static list <student> parsestudentfromjson (string data ){

Type listtype = new typetoken <shortlist <student> (){

}. GetType ();

Gson = new gson ();

Required list <student> List = gson. fromjson (data, listtype );

Return list;

}

}

Student in it is a JavaBean object:

 

Public class student {

Private string name;

Private int age;

Private string ID;

 

Public student (){

Super ();

}

 

Public student (string name, int age, string ID ){

Super ();

This. Name = Name;

This. Age = age;

This. ID = ID;

}

 

Public String getname (){

Return name;

}

 

Public void setname (string name ){

This. Name = Name;

}

 

Public int getage (){

Return age;

}

 

Public void setage (INT age ){

This. Age = age;

}

 

Public String GETID (){

Return ID;

}

 

Public void setid (string ID ){

This. ID = ID;

}

 

}

Let's look at the activity that we want to parse network data:

Public class mainactivity extends activity {

Private textview;

Private list <student> list;

 

/** Called when the activity is first created .*/

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Textview = (textview) findviewbyid (R. Id. textview );

String data = NULL;

Try {

Data = httputils

. Getdata ("http: // 10.16.12.165: 8080/jsontest/jsontestservlet ");

} Catch (exception e ){

E. printstacktrace ();

}

String result = "";

List = jsonutils. parsestudentfromjson (data );

For (student s: List ){

Result + = "name:" + S. getname () + "" + "Age:" + S. getage ()

+ "" + "ID:" + S. GETID () + "\ n ";

}

Textview. settext (result );

}

}

In this way, the network data can be obtained and parsed and used. The running result is as follows:

 

 

In addition, there is a good article: http://www.2cto.com/kf/201110/109534.html

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.