tpClient获取SocketTimeoutException

tpClient获取SocketTimeoutException

本文介绍了使用loopj AsyncHttpClient获取SocketTimeoutExceptions ...是否可以设置超时值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用loopj AsyncHttpClient 库时,在制作时我不断收到java.net.SocketTimeoutExceptions请求(请参见下文).

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.

堆栈跟踪:

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)

推荐答案

我发现AsyncHttpClient实际上默认为10秒超时.如果您的请求花费的时间更长,则会看到抛出SocketTimeoutException的信息.

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


编辑 :(感谢Horkavlna!)


Edit: (thanks, Horkavlna!)

您可以在javadoc中查看该方法的详细信息- http://loopj.com/android-async-http/doc/com/loopj/android/http/AsyncHttpClient.html#setTimeout(int)

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)

这篇关于使用loopj AsyncHttpClient获取SocketTimeoutExceptions ...是否可以设置超时值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:24