7

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?

Community
  • 1
  • 1
jellyfish
  • 7,868
  • 11
  • 37
  • 49

4 Answers4

9

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);
theomega
  • 31,591
  • 21
  • 89
  • 127
  • 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? – jellyfish Apr 20 '11 at 12:29
  • Do you really need the features of AndroidHTTPClient? Otherwise you could simply use DefaultHTTPClient. – theomega Apr 20 '11 at 12:31
  • actually, I don't really know the difference... this probably means "no", doesn't it? – jellyfish Apr 20 '11 at 12:33
  • Yes, perhaps, see this answer: http://stackoverflow.com/questions/5135918/androidhttpclient-and-defaulthttpclient – theomega Apr 20 '11 at 12:36
  • 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. – jellyfish Apr 20 '11 at 12:44
  • 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. – theomega Apr 20 '11 at 12:54
  • solved it by adding one simple line, but as you hinted out what was wrong initially, thank you anyway! – jellyfish Apr 21 '11 at 12:03
6

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.

jellyfish
  • 7,868
  • 11
  • 37
  • 49
5

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

  • I like that. Here's a more detailed explanation: http://www.intertech.com/Blog/Post/Android-DefaultHttpClientAndroidHttpClient-and-HttpParams.aspx – Sofi Software LLC Feb 22 '13 at 23:04
0

After reading around, here's how I did it using the params straight from the default client:

HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);

Original credit goes to http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/

Learn OpenGL ES
  • 4,759
  • 1
  • 36
  • 38