The source code is as follows:
PackageCom.demo.app.api;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.io.UnsupportedEncodingException;Importjava.util.ArrayList;ImportJava.util.HashMap;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.client.ClientProtocolException;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.methods.HttpGet;Importorg.apache.http.impl.client.DefaultHttpClient;ImportOrg.json.JSONArray;Importorg.json.JSONException;ImportOrg.json.JSONObject;ImportAndroid.util.Log; Public classJsonprovider {/*** Parse * *@throwsjsonexception*/ Private StaticArraylistAnalysis (String jsonstr)throwsjsonexception {/******************* parsing ***********************/Jsonarray Jsonarray=NULL; //initializing the list array objectarraylistNewArraylist(); Jsonarray=NewJsonarray (JSONSTR); for(inti = 0; I < jsonarray.length (); i++) {Jsonobject jsonobject=Jsonarray.getjsonobject (i); //initializing the map array objecthashmap<string, object> map =NewHashmap<string, object>(); Map.put ("title", Jsonobject.getstring ("title")); List.add (map); } returnlist; } Public Staticstring getjsondata (string url)throwsclientprotocolexception, IOException {String result= ""; HttpGet HttpGet=Newhttpget (URL); HttpClient HttpClient=Newdefaulthttpclient (); HttpResponse HttpResponse=NULL; Try{HttpResponse=Httpclient.execute (HttpGet); Httpentity httpentity=httpresponse.getentity (); if(Httpentity! =NULL) {InputStream InputStream=httpentity.getcontent (); Result=convertstreamtostring (InputStream); } } Catch(clientprotocolexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {Throwe; } finally{Httpclient.getconnectionmanager (). Shutdown (); HttpResponse=NULL; } returnresult; } Public StaticString convertstreamtostring (InputStream is) {BufferedReader reader=NULL; Try{Reader=NewBufferedReader (NewInputStreamReader (IS, "GBK"),//prevents garbled characters on the emulator512 * 1024); } Catch(unsupportedencodingexception E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } StringBuilder SB=NewStringBuilder (); String Line=NULL; Try { while(line = Reader.readline ())! =NULL) {sb.append ( line+ "/n"); } } Catch(IOException e) {LOG.E ("Dataprovier convertstreamtostring", E.getlocalizedmessage (), E); } finally { Try{is.close (); } Catch(IOException e) {e.printstacktrace (); } } returnsb.tostring (); }}
Source analysis of "Listviewjson" "Com.demo.app.api" "Jsonprovider" and its role in engineering