gpt4 book ai didi

android - 使用 loopj AsyncHttpClient 获取 SocketTimeoutExceptions ...我可以设置超时值吗?

转载 作者:IT老高 更新时间:2023-10-28 23:29:37 24 4
gpt4 key购买 nike

当使用 loopj AsyncHttpClient库,我在发出请求时不断收到 java.net.SocketTimeoutExceptions(见下文)。

我可以设置一些超时值吗?

注意:我发布此内容是希望为其他人提供一些帮助。我(愚蠢地)努力寻找解决方案一段时间。

堆栈跟踪:

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。

调整这个非常简单。只需执行以下操作:

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

编辑:(感谢 Horkavlna!)

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

关于android - 使用 loopj AsyncHttpClient 获取 SocketTimeoutExceptions ...我可以设置超时值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16223286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com