Android provides two time-out setting options for HTTP parameter settings via the Httpconnectionparams class, namely Setsotimeout and Setconnectiontimeout. Take a look at the Android official document a lack of clarity about the specific meaning of this two method makes it a decision to test the way in which these two parameters work.
After testing, Setconnectiontimeout sets the timeout for establishing a connection, which is for the three handshake of TCP, if a TCP connection to the HTTP server cannot be established within the specified time. Will throw connectiontimeoutexception. Setsotimeout is set to the TCP keepalive time, after the connection has been established for a specified period of time without receiving the corresponding packets sent from the server, then throw sockettimeoutexception.
First, the test code
PrivateString urlstring; Private intTimeout1,timeout2; Myasynctask (String URL,intTIMEOUT1,inttimeout2) {urlstring=URL; This. timeout1=timeout1; This. timeout2=Timeout2; } @Overrideprotectedstring Doinbackground (String ... params) {httpget (URLSTRING,TIMEOUT1,TIMEOUT2); return"Test"; } protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); NewMyasynctask ("http://www.xiaonei.com:1234", 3000,5000). Execute ("Start")); NewMyasynctask ("http://www.baidu.com", 3000,5000). Execute ("Start")); } Private voidHttpGet (String Url,intTIMEOUT1,inttimeout2) {LOG.V ("HttpGet", "HttpGet start Timeout1 is" +timeout1+ "timeout2" +timeout2); intTimeoutconnection = TIMEOUT1;//until connection is established intTimeoutsocket = Timeout2;//timeout for waiting for dataHttpparams httpparameters =NewBasichttpparams (); Httpconnectionparams.setconnectiontimeout (Httpparameters, timeoutconnection); Httpconnectionparams.setsotimeout (Httpparameters, Timeoutsocket); Defaulthttpclient httpClient=Newdefaulthttpclient (httpparameters); HttpGet Postrequest=NewHttpGet (URL); HttpResponse Httpresp=NULL; Try{Httpresp=Httpclient.execute (postrequest); } Catch(clientprotocolexception e) {log.v ("Main", "Clinet protocol Exception"); return; }Catch(sockettimeoutexception e) {//Todo:handle ExceptionLOG.V ("Main", "Socket timeout" +timeout2); return; }Catch(connecttimeoutexception e) {//Todo:handle ExceptionLOG.V ("Main", "Connection timeout" +timeout1); return; } Catch(IOException e) {log.v ("Main", "IO exception"); return; } }
Second, setconnectiontimeout test
Write an address in the HttpGet URL that cannot be communicated such as "10.1.1.1" or a public server that does not have an open port number such as "www.xiaonei.com:1234" will appear connnectiontimeoutexception. For an IP address that cannot be communicated, the Android system constructs a connection packet directly to a TCP that is sent to the address, but because the address does not exist, it does not receive confirmation that a timeout event occurs at a set time-out. It is basically similar to the case where the port number does not exist.
Third, setsotimeout test
Setsotimeout sets the keepalive time-out period for the socket. The test method here is to set up an agent for the Android terminal, and after the agent receives an HTTP request from the Android terminal, intercepts the HTTP request without sending it. To the timeout time thrown sockettimeoutexception. Such a way of intercepting HTTP packets can be easily implemented by fiddler. Using the Fiddler proxy function to androidhttp the packet capture method see the Http://www.oschina.net/question/221817_129716,fiddler interception data packet method see/HTTP www.mzwu.com/article.asp?id=3509.
After configuring the Fiddler Agent and intercepting the HTTP packets, run the test program to get the following output. Two HTTP requests output a socket timeout. The first request also outputs the socket timeout because the connection between the Android terminal and the agent is established properly, so there is no connection timeout, but the agent does not get a response after the HTTP request sent by the Android terminal. Can not be the corresponding information to the client, the client has a socket timeout.
Iv. further understanding of Asynctask
As can be seen from the above test results, although the OnCreate function calls the Asynctask class to send two different HTTP requests, the second called Asyntask needs to wait until the first called timeout before it can be executed. This is because Android, after 3.0, asynctask all the asynctask into one thread and executes in a certain order. A more detailed analysis can refer to the http://blog.csdn.net/singwhatiwanna/article/details/17596225.
V. reference materials
[1] Explanation of HTTP timeout parameters: http://stackoverflow.com/a/18185568/1767800
[2] about how to get the HTTP connection timeout: Http://stackoverflow.com/questions/100841/artificially-create-a-connection-timeout-error