JSON is a lightweight object with small data size, convenient transmission, and easy to parse!
Create a new tool class JsonUtil to obtain the data returned by the request.
Copy codeThe Code is as follows: public class JsonUtil {
Private static final String TAG = "JSONUTIL ";
Public static JSONObject getJSON (String url) throws Exception {
Return new JSONObject (getRequest (url ));
}
Protected static String getRequest (String url ){
Return getRequest (url, new DefaultHttpClient (new BasicHttpParams ()));
}
Protected static String getRequest (String url, DefaultHttpClient client ){
String result = null;
Int statusCode = 0;
HttpGet httpGet = new HttpGet (url );
Try {
HttpResponse httpResponse = client.exe cute (httpGet );
StatusCode = httpResponse. getStatusLine (). getStatusCode (); // If statusCode is 200, the request data is successful.
Result = parseInputStream (httpResponse. getEntity ());
} Catch (ClientProtocolException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
HttpGet. abort ();
}
Return result;
}
Private static String parseInputStream (HttpEntity entity ){
StringBuilder sb = null;
Try {
Sb = new StringBuilder ("");
InputStream inputStream = entity. getContent ();
Int length = 0;
Byte [] buffer = new byte [1024];
While (length = inputStream. read (buffer)>-1 ){
Sb. append (new String (buffer, 0, length ));
}
Return sb. toString ();
} Catch (IllegalStateException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return sb. toString ();
}
}
Obtain and parse data:
Note: The simulator cannot use localhost: 8080 or 127.0.0.1: 8080 to access the website on its own computer, because the simulator sets the simulator itself as localhost by default, therefore, if this method is set, the access to the simulator itself will be performed. We need to change the host name to 10.0.2.2, which is the host name set by the simulator to access your computer. It records the name of your computer.
In addition, to obtain data, you need to encapsulate the following method into a new thread, which cannot be placed in the main thread of the program!Copy codeThe Code is as follows:/* http: // 10.0.2.2: 8080/index. jsp
* {Students: [{name: 'livingstone ', age: 25}, {name: 'Ls', age: 26}], class: '09gis '}
*/
Private void Livingstone (){
Try {
String URL = "http: // 10.0.2.2: 8080/index. jsp ";
// Obtain the JSON object returned by the background --> {students: [{name: 'livingstone ', age: 25}, {name: 'Ls', age: 26}], class: '09gis class '}
JSONObject jObj = JsonUtil. getJSON (URL );
// Obtain the student array --> students: [{name: 'livingstone ', age: 25}, {name: 'Ls', age: 26}]
JSONArray jArr = jObj. getJSONArray ("students ");
// Obtain the class --> class: '09gis class'
String classname = jObj. getString ("class ");
// Obtain the JSON object of the first student based on the index --> {name: 'livingstone ', age: 25}
JSONObject j1 = jArr. getJSONObject (0 );
String studentInfo = jArr. length () + "Students" + j1.getString ("name ")
+ J1.getInt ("age ");
} Catch (Exception e ){
E. printStackTrace ();
}
}