18

When using the loopj AsyncHttpClient library, I keep getting java.net.SocketTimeoutExceptions when making requests (see below).

Is there some timeout value I can set?

Note: I'm posting this to hopefully provide some help for other people. I (stupidly) struggled to find the solution for some time.

Stack trace:

java.net.SocketTimeoutException
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:76)
at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:95)
at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:57)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
loeschg
  • 29,961
  • 26
  • 97
  • 150

1 Answers1

41

I discovered that the AsyncHttpClient actually defaults to a 10 second timeout. If your request takes longer you'll see the SocketTimeoutException thrown.

Adjusting this is really simple. Just do the following:

final int DEFAULT_TIMEOUT = 20 * 1000;
AsyncHttpClient aClient = new AsyncHttpClient();
aClient.setTimeout(DEFAULT_TIMEOUT);
//... continue as normal

Edit: (thanks, Horkavlna!)

You can see the details of the method in the javadoc - http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html#setTimeout(int)

loeschg
  • 29,961
  • 26
  • 97
  • 150
  • 1
    Is the timeout in milliseconds? There seems to be no documentation on this at all. – Manuel Oct 03 '13 at 19:02
  • 1
    @dragon112 The timeout is in milliseconds. In the example above I'm setting the timeout to 20 seconds. – loeschg Oct 08 '13 at 15:04
  • 1
    you can find it in JAVADOC http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html#setTimeout(int) – horkavlna May 01 '14 at 10:21
  • 1
    aClient.setResponseTimeout(60000); aClient.setConnectTimeout(60000); these are some useful methods also – Iftikar Urrhman Khan Jan 30 '16 at 18:19
  • what are the differences from `setTimeout()` to `setResponseTimeout` ? Thats what I'm finding documentation for. – patrickjason91 Jun 10 '16 at 01:21
  • @patrickjason91 doc says: setTimeout(int value) - Set both the connection and socket timeouts. So setResponseTimeout is for waiting while setTimeout is for requesting... – Osify Oct 11 '16 at 04:23