Idle no matter, encapsulated a HttpClient request tool class, including the timeout time, set up the agent, support HTTP and HTTPS, has got and post two network request mode, copy code to your project can be used!!
Package Com.mars.test;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import java.io.InputStream; Import Java.util.hashmap;import java.util.list;import Org.apache.http.httphost;import org.apache.http.HttpResponse ; 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.conn.params.connmanagerparams;import Org.apache.http.conn.params.connperroutebean;import Org.apache.http.conn.params.connroutepnames;import Org.apache.http.conn.scheme.plainsocketfactory;import Org.apache.http.conn.scheme.scheme;import Org.apache.http.conn.scheme.schemeregistry;import Org.apache.http.conn.ssl.sslsocketfactory;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.impl.conn.tsccm.threadsafeclientconnmanager;import Org.apache.http.message.BasicNameValuePair; ImporT Org.apache.http.params.basichttpparams;import org.apache.http.params.coreprotocolpnames;import Org.apache.http.params.httpconnectionparams;import Org.apache.http.protocol.http;import Android.content.Context; Import Android.net.connectivitymanager;import Android.net.networkinfo;import Android.net.proxy;import  android.text.textutils;/** * HTTP Request Tool Class * * @author Administrator * */public class Httputils {private static final String TAG = "Httputils";p rivate static final int default_socket_buffer_size = 8 * 1024; 8KBprivate static final int max_connections = 10; Maximum number of concurrent connections for HTTP requests private static final int socket_timeout = 55 * 1000; 55sprivate static final String map_key_apn = "APN";p rivate static final String map_key_apn_proxy = "Apn_proxy";p rivate Static final String Map_key_apn_port = "Apn_port"; @SuppressWarnings ("deprecation") public static hashmap<string, Object> Getnetinfo (Context context) {hashmap<string, object> HashMap = null; Connectivitymanager Manager = (connectivItymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo activenetworkinfo = Manager.getactivenetworkinfo (); if (null! = Activenetworkinfo) {if ( Connectivitymanager.type_mobile = = Activenetworkinfo.gettype ()) {String APN = Activenetworkinfo.getextrainfo (); String apnproxy = Proxy.getdefaulthost (); int apnport = Proxy.getdefaultport (); hashMap = new hashmap<string, Object > (); Hashmap.put (MAP_KEY_APN, APN), Hashmap.put (Map_key_apn_proxy, Apnproxy); Hashmap.put (Map_key_apn_port, Apnport);}} return HASHMAP;} /** * Create and Initialize httpclient Object * * @return */public static Defaulthttpclient Create (context context, String URL) {DEFAULTHTTPCL Ient httpClient = null; Basichttpparams httpparams = new Basichttpparams (); Connmanagerparams.settimeout (Httpparams, socket_timeout); Connmanagerparams.setmaxconnectionsperroute (httpparams,new Connperroutebean (max_connections)); Connmanagerparams.setmaxtotalconnections (Httpparams, 10); Httpconnectionparams.setsotimeout (Httpparams, socket_timeout); HttpcOnnectionparams.setconnectiontimeout (Httpparams, socket_timeout); Httpconnectionparams.settcpnodelay (Httpparams, true); Httpconnectionparams.setsocketbuffersize (httpparams,default_socket_buffer_size); String AgentProperty = System.getproperty ("http.agent"); Textutils.isempty (AgentProperty)) {Httpparams.setparameter (Coreprotocolpnames.user_agent,system.getproperty (" Http.agent "));} if (null! = URL && url.startswith ("https://")) {//support HTTP with httpsschemeregistry schemeregistry = new Schemeregistry (); Schemeregistry.register (New Scheme ("http", Plainsocketfactory.getsocketfactory (), +)); Schemeregistry.register (New Scheme ("https", Sslsocketfactory.getsocketfactory (), 443));// Threadsafeclientconnmanager Thread safety Management class Threadsafeclientconnmanager cm = new Threadsafeclientconnmanager (HttpParams, Schemeregistry); httpClient = new Defaulthttpclient (cm, httpparams);} else {httpClient = new defaulthttpclient (httpparams);} hashmap<string, object> netInfo = getnetinfo (context); if (null! = NetInfo) {String APN = (string) netinfo.get (MAP_KEY_APN); String apnproxy = (string) netinfo.get (map_key_apn_proxy); int apnport = (Integer) netinfo.get (map_key_apn_port);// Determine if you need to set proxy information if (null! = APN &&! "). Equals (Apnproxy) && null! = apnproxy&&-1! = Apnport) {//Set proxy information Httphost host = new Httphost (Apnproxy, APN Port); Httpclient.getparams (). Setparameter (Connroutepnames.default_proxy, Host);}} return httpClient;} /** * Send JSON to Background server * * @param jsonstring * @return The response data that the server may return * @throws IOException * @throws illegalstateexception * /public static string Sendjson2server (context context, string jsonstring,list<basicnamevaluepair> params, string sendaddress) throws IllegalStateException, IOException {if (null! = sendaddress) {HttpPost post = new HttpPost (sendaddress );//bytearrayentity entity = NULL; Bytearrayoutputstream out = Null;inputstream in = null; Logutil.info (TAG, "sending data to" + sendaddress); try {//entity = new Bytearrayentity (jsonstring.getbytes ("Utf-8"));p OSt.setentity (New urlencodedformentity (params, HTTP. Utf_8)); HttpClient client = Create (context, sendaddress); HttpResponse response = Client.execute (post); int responsecode = Response.getstatusline (). Getstatuscode (); Logutil.info (TAG, "Response code:" + responsecode); if (= = Responsecode) {in = Response.getentity (). getcontent (); byte [] buf = new Byte[1024];int len;out = new Bytearrayoutputstream (); while ( -1! = (len = in.read (buf))) {Out.write (buf, 0, le n);} byte[] ByteArray = Out.tobytearray (); return new String (ByteArray, "Utf-8");}} finally {if (null! = out) {try {out.close ();} catch (IOException e) {e.printstacktrace ()}} if (null! = in) {try {in.close ()} catch (IOException e) {e.printstacktrace ();}}}} return null;} /** * Get data from server * * @return * @throws ioexception */public static string Getdatafromserver (context context, String URL) thr oWS IOException {HttpGet get = new HttpGet (URL); HttpResponse execute = NULL; Bytearrayoutputstream out = Null;inputstream content = null;try {HttpClient HTTpclient = Create (context, url), execute = httpclient.execute (get), if (+ = = Execute.getstatusline (). Getstatuscode ()) { Content = Execute.getentity (). GetContent (); out = new Bytearrayoutputstream (); byte[] buf = new Byte[1024];int len;while (- 1! = (len = content.read (buf))) {Out.write (buf, 0, Len);} return new String (Out.tobytearray (), "Utf-8");}} Finally {if (out! = null) {Out.close ();} if (content! = null) {Content.close ();}} return null;}}
HTTP request Tool Class