Timeout in DefaultHttpClient Class Android

前端 未结 2 2018
生来不讨喜
生来不讨喜 2020-12-16 01:28

I have created an Android application where I connect to a remote server php file to retrieve some information. Below is the code for that.

Here I want to add timeo

相关标签:
2条回答
  • 2020-12-16 02:18

    Use the HttpConnectionParams of your DefaultHttpClient::

    final HttpParams httpParameters = yourHttpClient.getParams();
    
    HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
    HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);
    
    0 讨论(0)
  • 2020-12-16 02:19
    final HttpParams httpParameters = yourHttpClient.getParams();
    
    HttpConnectionParams.setConnectionTimeout(httpParameters, connectionTimeOutSec * 1000);
    HttpConnectionParams.setSoTimeout        (httpParameters, socketTimeoutSec * 1000);
    

    If that does not work (as in my case). try this which works for me (link)

    HttpGet httpGet = new HttpGet(url);
    HttpParams httpParameters = new BasicHttpParams();
    
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 5000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    
    0 讨论(0)
提交回复
热议问题