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