First, a tool class
package com.luo.utils;import java.io.ioexception;import java.io.inputstream;import Java.io.unsupportedencodingexception;import java.net.urldecoder;import java.util.iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.http.httpentity;import org.apache.http.httpresponse;import org.apache.http.httpstatus; Import org.apache.http.namevaluepair;import org.apache.http.client.clientprotocolexception;import org.apache.http.client.httpclient;import org.apache.http.client.entity.urlencodedformentity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.params.httpclientparams;import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.params.basichttpparams;import org.apache.http.params.httpconnectionparams; Import org.apache.http.params.httpparams;import org.apache.http.params.httpprotocolparams;import org.apache.http.protocol.http;import org.apache.http.util.entityutils;import android.content.context;import android.graphics.bitmap; import android.graphics.bitmapfactory;import android.net.connectivitymanager;import android.net.http.androidhttpclient;import android.util.log;import android.widget.toast;/** Network Connection * @author cnmobi-db * */public class MyConnect {private static Httpparams httpparams;private static httpclient httpclient;/**get Way Request * @param url * @param params * @return */public static string doget ( String url, map<string, string> params) { /* build HttpGet Object */ string paramstr = ""; Set<map.entry<string, string>> set = params.entryset (); for ( Iterator<map.enTry<string, string>> it = set.iterator (); it.hasnext ();) { map.entry<string, string> entry = (map.entry<string, string>) it.next (); system.out.println (Entry.getkey () + "--->" + Entry.getvalue ()); paramstr += paramStr = "&" + entry.getkey () + "=" + entry.getvalue (); }if (!paramstr.equals (")") {paramStr = Paramstr.replacefirst ("&", "?"); Url += paramstr;} LOG.D ("Strresult", url); Httpget httprequest = new httpget (URL); string strresult = "Dogeterror";try {/* sends the request and waits for a response */httpresponse hTtpresponse = httpclient.execute (HttpRequest);/* If the status code is 200 ok */if ( Httpresponse.getstatusline (). Getstatuscode () == 200) {/* Read back data */strresult = entityutils.tostring (Httpresponse.getentity ());} else {//strResult = "error response: "//+ httpresponse.getstatusline (). ToString ();strresult = "404";}} catch (clientprotocolexception e) {//strresult = e.getmessage (). toString (); strresult = "404"; E.printstacktrace ();} catch (ioexception e) {//strresult = e.getmessage (). toString (); strresult = "404"; E.printstacktrace ();} catch (exception e) {//strresult = e.getmessage (). toString (); strresult = "404"; E.printstacktrace ();} LOG.D ("Strresult", strresult); return strresult;} /**post Way Request * @param session * @param url * @param params * @return */public static string dopost (string session,string url , list<namevaluepair> params) {string www = url + "?"; For (Int i =0; i<params.size (); i++) {if (I != params.size ()-1) www = www + params.get (i). ToString () + "&"; Elsewww = www + params.get (i). ToString ();} LOG.D ("Strresult", "url---> " +www);/* build HttpPost objects */HttpPost httpRequest = New httppost (URL); string strresult = "Doposterror";try {/* add request parameter to request object */httprequest.setentity (new urlencodedformentity (params, http. Utf_8)), if (session != null) { httprequest.setheader ("Cookie") , session); SYSTEM.OUT.PRINTLN (session);} /* sends a request and waits for a response */httpresponse httpresponse = httpclient.execute (httpRequest);/* If the status code is 200 ok */if (Httpresponse.getstatusline (). Getstatuscode () == 200) {/* Read back data */strresult = entityutils.tostring (Httpresponse.getentity (), HTTP. utf_8); } else {strresult = "404";// "error response: " +// Httpresponse.getstatusline (). toString ();}} catch (unsupportedencodingexception e) {strResult = "404";// e.getmessage ( ). toString (); E.printstacktrace ();} catch (clientprotocolexception e) {strResult = "404";// e.getmessage (). ToString (); E.printstacktrace ();} catch (ioexception e) {strResult = "404";// e.getmessage (). toString (); E.printstacktrace ();} catch (exception e) {strResult = "404";// e.getmessage (). toString (); E.printstacktrace ();} LOG.D ("Strresult", strresult); Try {strresult = urldecoder.decode (StrResult, HTTP. Utf_8);} catch (Unsupportedencodingexception e) {// todo auto-generated catch blocke.printstacktrace ();} LOG.D ("Strresult", strresult); return strresult;} /** configuration httpclient * @return */public static httpclient gethttpclient () {// Create HttpParams to use to set HTTP parameters (this part is not required) httpparams = new Basichttpparams ();// set connection timeout and Socket timeout, and Socket Cache size Httpconnectionparams.setconnectiontimeout (httpparams, 20 * 1000); Httpconnectionparams.setsotimeout (httpparams, 20 * 1000); Httpconnectionparams.setsocketbuffersize (httpparams, 8192);// set redirection, default = Truehttpclientparams.setredirecting (httpparams, true);// settings user agentString useragent = "mozilla/5.0 (WINDOWS;&NBSP;U;&NBSP;WINDOWS&NBSP;NT&NBSP;5.1;&NBSP;ZH-CN;&NBSP;RV : 1.9.2) gecko/20100115 firefox/3.6 "; Httpprotocolparams.setuseragent (httpparams, useragent);// Create an HttpClient instance httpclient = new defaulthttpclient (httpparams);return HttpClient;} /** Get network connectivity status * @param context * @return */public static boolean NetworkStatus (Context context) {boolean netSataus = false; connectivitymanager cwjmanager = (Connectivitymanager) context.getsystemservice ( Context.connectivity_service); Cwjmanager.getactivenetworkinfo ();if (Cwjmanager.getactivenetworkinfo () != null) {netsataus = cwjmanager.getactivenetworkinfo (). isAvailable ();} if (!netsataus) Toast.maketext (context, "Network error! ", toast.length_short). Show (); Return netsataus;} /** Get network pictures * @param url * @return */public static Bitmap Loadimagefrominternet (String url) {Bitmap bitmap = null; HttpcLient client = androidhttpclient.newinstance ("Android"); Httpparams params = client.getparams (); Httpconnectionparams.setconnectiontimeout (params, 3000); Httpconnectionparams.setsocketbuffersize (params, 3000); httpresponse response = null;inputstream inputstream = null; Httpget httpget = null;try {httpget = new httpget (URL); response = client.execute (HttpGet); Int statecode = response.getstatusline (). GetStatusCode ();if (STATECODE&NBSP;!=&NBSP;HTTPSTATUS.SC_OK) {return bitmap;} Httpentity entity = response.getentity ();if (entity != null) {try { Inputstream = entity.getcontent (); Return bitmap = bitmapfactory.decodestream ( InputStream);} finally {if (Inputstream != null) {inputstream.close ();} Entity.consumecontent ();}}} catch (CLIENTPROTOCOLEXCEPTION&NBsp;e) {httpget.abort (); E.printstacktrace ();} catch (ioexception e) {httpget.abort (); E.printstacktrace ();} finally {((androidhttpclient) client). Close ();} Return bitmap;}}
Then create an asynchronous task inner class
class gethispetsasynctask extends asynctask<object, object, object> { Progressdialog dialog = progressdialog.show (Heactivity.this, null, "Searching for pet information, please later ..."); private string url;private string token;private string uid;public Gethispetsasynctask (String url, string token, string uid) {this.url = Url;this.token = token;this.uid = uid;} @Overrideprotected object doinbackground (object... params) {String code = " 1 "; Myconnect.gethttpclient (); Map<string, string> parms = new linkedhashmap<string, string> (); Parms.put ("token", token);p arms.put ("UID", uid); String jsoncontent = myconnect.doget (url, parms); try {jsonobject jsonobject = new jsonobject (jsoncontent);if (jsonobject != null) {code = jSonobject.getstring ("code");if (Jsonobject.has ("Petarray")) {String j1 = Jsonobject.getstring ("Petarray"); Jsonarray j2 = new jsonarray (J1);for (int i = 0; i < j2.length (); i++) {jsonobject jsonobject2 = j2.getjsonobject (i); String j3 = jsonobject2.getint ("Weight") + " kg"; String j4 = jsonobject2.getstring ("nickname"); String j5 = jsonobject2.getstring ("Birthday"); String j6 = jsonobject2.getstring ("breed"); Hashmap<string, string> map = new hashmap<string, string> (); Map.put ( "Weight", j3), Map.put ("name", j4), Map.put ("Bir", j5), Map.put ("kind", j6); Hispetlist.add ( map);}} if (Jsonobject.has ("Terminalarray")) {string ter = jsonobject.getstring (" Terminalarray "); Jsonarray termes = new jsonarray (TER);for (int i = 0; i < termes.length (); i++) {jsonobject jsonobject3 = termes.getjsonobject (i); String terid = jsonobject3.getstring ("Terminalid"); String dist = jsonobject3.getstring ("Dist"); Hashmap<string, string> map1 = new hashmap<string, string> (); Map1.put ("Terminalid", terid); Map1.put ("Dist", dist); Histerlist.add (MAP1);}}} catch (jsonexception e) {e.printstacktrace ();} Return code;} @Overrideprotected void onpostexecute (Object result) {super.onpostexecute (result); SYSTEM.OUT.PRINTLN ("-----result------>" + result);d Ialog.dismiss ();if (Result.equals ("0")) {hispetapter = new simpleadapter (heactivity.this, hispetlist,r.layout.list_ itemhispet, new string[] { "Weight", "bir", "kind", "name" }, new int[] {r.id.weight, r.id.bir, r.id.kind, r.id.name Listview.setadapter (hispetapter);histerapter = new Simpleadapter (heactivity.this, histerlist,r.layout.list_itemhister, new string[] { " Terminalid "," Dist "&NBSP;},&NBSP;NEW&NBSP;INT[]&NBSP;{&NBSP;R.ID.TER,&NBSP;R.ID.DIST&NBSP;}); Listview2.setadapter (Histerapter);} else if (Result.equals ("40008")) {toast.maketext (heactivity.this, "Identity expired, please sign in again", Toast.length_short). Show ();} else {}}}
Finally start the asynchronous task
New Gethispetsasynctask (Constantutils.host1 + constantutils.url_26,constantutils.token, uid + ""). Execute (NULL, NULL, NULL);
Android Interface Request Instance