Android: AndroidHttpClient - how to set timeout?

24,342

Solution 1

I did miss to attach the params to my http request, but the proper way to do this in my example is

httpGet.setParams(httpParams);

before calling httpClient.execute(httpGet).

Just added that line and it worked fine.

Solution 2

You do not use the httpParams params, they must be provided to the HTTPClient. So it won't work like this. In the anwer you linked, the order is correct! Try the following order: Create the Params first and supply them to the HTTPClient.

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 10000);

HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet(url);

HttpResponse response = client.execute(request);

Solution 3

The other option to set on the client itself:

AndroidHttpClient client = AndroidHttpClient.newInstance("Client V/1.0");
HttpConnectionParams.setConnectionTimeout(this.client.getParams(), 3000);
HttpConnectionParams.setSoTimeout(this.client.getParams(), 5000);

This should result in those particular params being set...

HTH

Share:
24,342
jellyfish
Author by

jellyfish

Far, far away from my 10.000 hours, but I'm always willing to learn! :-) Atm working with Google Android.

Updated on January 15, 2020

Comments

  • jellyfish
    jellyfish over 4 years

    I have followed the instructions of kuester2000's answer, but my timeout settings don't seem to work.

    try
    {
        int timeout = 3000;
        URL myURL = //some valid URL
    
        AndroidHttpClient = AndroidHttpClient.newInstance("name");
        HttpGet httpGet = new HttpGet(myURL.toExternalForm());
    
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        HttpConnectionParams.setSoTimeout(httpParams, timeout);
    
        HttpResponse response = httpClient.execute(httpGet);
    
        //...
    }
    catch (SocketTimeoutException e)
    {
        e.printStackTrace();
    }
    catch (ConnectTimeoutException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    //...
    

    However, the timeout value doesn't change anything.

    In the answer I linked, it also says:

    The connection timeout throws "java.net.SocketTimeoutException: Socket is not connected" and the socket timeout "java.net.SocketTimeoutException: The operation timed out".

    But I get neither. Instead I get "org.apache.http.conn.ConnectTimeoutException: Connect to ... timed out"

    so can anybody help me? where is the mistake?

  • jellyfish
    jellyfish about 13 years
    I just realized thanks to your answer that I forgot to insert httpClient.setParams(httpParameters);, which was suggested by kuester2000. However, it's not working with AndroidHttpClient. Is it possible to use AndroidHttpClient instead of HttpClient the way you do?
  • theomega
    theomega about 13 years
    Do you really need the features of AndroidHTTPClient? Otherwise you could simply use DefaultHTTPClient.
  • jellyfish
    jellyfish about 13 years
    actually, I don't really know the difference... this probably means "no", doesn't it?
  • theomega
    theomega about 13 years
    Yes, perhaps, see this answer: stackoverflow.com/questions/5135918/…
  • jellyfish
    jellyfish about 13 years
    I did get that far, but "reasonable default settings" is not really helpful to me. ^^ However, it says in the documentation at the "newInstance" method: "Create a new HttpClient with reasonable defaults (which you can update)." but I can't find a way to update them.
  • theomega
    theomega about 13 years
    You have to choose: Either you let Android choose the defaults (including the timouts), or set the settings yourself. If you realy need configurable timeouts, then take the DefaultHTTPClient.
  • jellyfish
    jellyfish about 13 years
    solved it by adding one simple line, but as you hinted out what was wrong initially, thank you anyway!
  • Sofi Software LLC
    Sofi Software LLC about 11 years
    I like that. Here's a more detailed explanation: intertech.com/Blog/Post/…