Http://www.open-open.com/lib/view/open1326868964593.html
A friend asked me how the network request timed out. I didn't do this at the time, so I thought it was try .... catch... an error occurred while retrieving the result. I found that the result was not obtained. Today I have time to study it. I found that it was obtained from the object in the response, next I will share the entity of my own urlconnection and httpclient network request response code with you. I hope it will help you!
Package COM. zhangke. product. platform. HTTP. JSON; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. outputstream; import Java. io. unsupportedencodingexception; import java.net. httpurlconnection; import java.net. inetsocketaddress; import java.net. proxy; import java.net. URL; import java.net. urlconnection; import Java. util. arraylis T; import Java. util. iterator; import Java. util. list; import Java. util. map; import Org. apache. HTTP. header; 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. httppost; import Org. apache. HTTP. conn. CLI Entconnectionrequest; import Org. apache. HTTP. conn. params. connroutepnames; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import Org. apache. HTTP. params. basichttpparams; import Org. apache. HTTP. params. httpconnectionparams; import Org. apache. HTTP. params. httpparams; import COM. zhangke. product. platform. util. networkutil; import android. content. context; imp ORT android. util. log;/*** @ author spring sky * QQ 840950105 * Email: vipa1888@163.com * copyright: spring sky * this class use in for request server and get server respnonse data ***/public class network {/*** network request response code ** <br> */private int responsecode = 1; /*** 408 indicates network timeout */public static final int request_timeout_code = 408;/*** request character encoding */Private Static final string charset = "UTF-8 "; /*** request server timeout */P Rivate static final int request_time_out = 1000*10;/***** data time when the response is read */Private Static final int read_time_out = 1000*5; private context; public Network (context) {super (); this. context = context;}/*** inputstream to string type * @ Param is * @ return */Public String getstring (inputstream is) {string STR = NULL; try {If (is! = NULL) {bufferedreader BR = new bufferedreader (New inputstreamreader (is, charset); string line = NULL; stringbuffer sb = new stringbuffer (); While (line = BR. readline ())! = NULL) {sb. append (line);} STR = sb. tostring (); If (Str. startswith ("<HTML>") // retrieves XML or JSON data. If the obtained data is XML, It is null {STR = NULL ;}}} catch (exception e) {e. printstacktrace ();} return STR;}/*** httpclient request type * @ Param requesturl * @ Param map * @ return */Public inputstream requesthttpclient (string requesturl, Map <string, string> map) {inputstream = NULL;/*** add timeout time */basichttpparams httppa Rams = new basichttpparams (); httpconnectionparams. setconnectiontimeout (httpparams, request_time_out); httpconnectionparams. setsotimeout (httpparams, read_time_out); httpclient = new defaulthttpclient (httpparams); If (networkutil. getnetworktype () = networkutil. wap_connected) {httphost proxy = new httphost ("10.0.0.172", 80); httpclient. getparams (). setparameter (connroutepnames. default_proxy, PR Oxy);} httppost = new httppost (requesturl); httppost. setheader ("charset", charset); httppost. setheader ("Content-Type", "application/X-WWW-form-urlencoded"); List <basicnamevaluepair> List = new arraylist <basicnamevaluepair> (); iterator <string> it = map. keyset (). iterator (); While (it. hasnext () {string key = it. next (); string value = map. get (key); log. E ("request server", key + "=" + value); list. add (New B Asicnamevaluepair (Key, value);} Try {httppost. setentity (New urlencodedformentity (list, charset); httpresponse response httpclient.exe cute (httppost); inputstream = response. getentity (). getcontent (); responsecode = response. getstatusline (). getstatuscode (); // get the response code log. E ("response code", response. getstatusline (). getstatuscode () + ""); // header [] headers = response. getallheaders (); // get the data in the header // For (int I = 0; I