In the HTTP request to access the interface, the use of some other people encapsulated class, but used to use, always not accustomed to, and sometimes problems, but also to find reasons, in short, all kinds of trouble, so I based on their actual project needs, wrote some I think is more practical class.
Package Com.jiayu.trafficlight.http;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.OutputStreamWriter ; Import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Java.net.urlconnection;import Java.util.arraylist;import Java.util.list;import Java.util.Map;import Org.apache.http.httpresponse;import Org.apache.http.namevaluepair;import org.apache.http.client.HttpClient; Import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httppost;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.protocol.http;import Org.apache.http.util.entityutils;import Org.json.JSONObject;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.asynctask;import Com.jiayu.trafficlight.util.config;import COM.JIAYU.TRAFFICLIGht.util.uploadutil;public class Netconnection {/** * Upgrade interface * * @param URL * @param method * @param successcallback * @pa Ram Failcallback * @param map */public netconnection (final String URL, final httpmethod method,final Successcallback succe Sscallback,final Failcallback Failcallback, Final map<string, string> Map) {New asynctask<void, Void, String > () {@Overrideprotected String doinbackground (Void ... params) {StringBuffer sb = new StringBuffer (); String realstring = ""; for (map.entry<string, string> entry:map.entrySet ()) {Sb.append (Entry.getkey ()). Append (" = "). Append (Entry.getvalue ()). Append (" & ");} realstring = sb.tostring (). substring (0,sb.tostring (). Length ()-1); try {URLConnection Uc;switch (method) {Case POST:UC = New URL (URL). OpenConnection (); Uc.setdooutput (true); Uc.setconnecttimeout (5000); BufferedWriter bw = new BufferedWriter (New OutputStreamWriter (Uc.getoutputstream (), Config.charset)); Bw.write ( realstring); Bw.flush (); break;default:uc = new URL (url + "?" + Realstring). OpenConnection (); Uc.setconnecttimeout, Uc.setrequestproperty ("Apikey", " 2f50fafc573e93a725e277b073d9c5dd "); break;} System.out.println ("Request URL:" + uc.geturl ()); System.out.println ("Request Date:" + realstring); System.out.println ("Result Status:" + ((httpurlconnection) UC). Getresponsecode ()); if (((httpurlconnection) UC). Getresponsecode () = = () {BufferedReader br = new BufferedReader (New InputStreamReader (Uc.getinputstream (), Config.charset)); StringBuffer result = new StringBuffer (); String line = "", while (line = Br.readline ())! = null) {result.append (line);} SYSTEM.OUT.PRINTLN ("Result:" + result); return result.tostring ();} else {System.out.println ("Result:" + ((httpurlconnection) UC). Getresponsecode ());}} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return null;} @Overrideprotected void OnPostExecute (String result) {if (result! = null) {try {jsonobject json = new Jsonobject (result); F (successcallback! = null) {successCallback.onsuccess (result);}} catch (Exception e) {e.printstacktrace (); }} else {if (failcallback! = null) {Failcallback.onfail ();}} Super.onpostexecute (result);}}. Execute ();} /** * JSON-type interface upgrade * * @param URL * @param method * @param successcallback * @param failcallback * @param map */public netc Onnection (Final string url,final successcallback successcallback,final failcallback failcallback, final string json) { New asynctask<void, Void, string> () {@Overrideprotected String doinbackground (void ... params) {try { HttpURLConnection UC = (httpurlconnection) new URL (URL). OpenConnection (); Uc.setdooutput (true); Uc.setrequestmethod (" POST "); Uc.setconnecttimeout (uc.setusecaches); Uc.setrequestproperty (" Content-type "," application/ JSON "); Uc.setrequestproperty (" Accept "," Application/json "); BufferedWriter bw = new BufferedWriter (New OutputStreamWriter (Uc.getoutputstream (), Config.charset)); Bw.write (JSON); Bw.flush (); System.out.println ("RequeSt URL: "+ uc.geturl ()); System.out.println ("Request Date:" + JSON), if (uc.getresponsecode () = =) {BufferedReader br = new BufferedReader (New in Putstreamreader (Uc.getinputstream (), config.charset)); StringBuffer result = new StringBuffer (); String line = "", while (line = Br.readline ())! = null) {result.append (line);} SYSTEM.OUT.PRINTLN ("Result:" + result); return result.tostring ();} else {System.out.println ("uc.getresponsecode () = =" + Uc.getresponsecode ()); System.out.println ("uc.getresponsemessage () = =" + uc.getresponsemessage ());}} catch (Malformedurlexception e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return null;} @Overrideprotected void OnPostExecute (String result) {if (result! = null) {if (successcallback! = null) {SUCCESSCALLBACK.O Nsuccess (result);}} else {if (failcallback! = null) {Failcallback.onfail ();}} Super.onpostexecute (result);}}. Execute ();} public static interface Successcallback {void onsuccess (String result); public static interface Failcallback {void Onfail ();}}
The class above is the HTTP provider class, here are some examples:
/** * points commodity list */private void InitData (int flag) {private String URL = "Http://192.168.1.11/xxx"; map<string, string> params = new hashmap<string, string> ();p arams.put ("Types", "0");p arams.put ("Isonsail" , "0");p arams.put ("Rowstart", "0");p arams.put ("Rowend", "a") ; new Netconnection (URL, "POST", new Netconnection.successcallback () {@Overridepublic void onsuccess (String result) {}}, new Netconnection.failcallback () { @Overridepublic void Onfail () {}}, params);
There is also a JSON-type entry:
/** * Launch full Vehicle Diagnostic Interface Description: This interface is used to send diagnostic instructions to IOBD devices */public void Check () {jsonobject param = new jsonobject (); try {param.put ("Appkey", Config.getappkey (this));p Aram.put (the "IMEI", Config.getimei (This));} catch (Exception e) {e.printstacktrace ();} New Netconnection (Config.server_url + "Diagnosis/trigge", new Netconnection.successcallback () {@Overridepublic void onsuccess (String result) {}}, new Netconnection.failcallback () {@Overridepublic void Onfail () {}}, param.tostring ());}
In fact, this class is also in the reference of the master and then according to my experience in the development of the project summed up, in fact, after the actual development of the test, this class can be directly used.
Encapsulation of the Android HTTP request access interface