我在使用Loopj异步HTTP库时遇到了一个奇怪的问题。我有时会使用get或POST获得响应,有时响应会在很长一段时间后出现,有时根本没有响应。只有在使用wifi时才会发生这种情况。我在很多项目中都使用过loopj,这些项目与wifi配合得很好,但这个项目没有。当我使用移动数据3G连接时,总会收到响应。我已经覆盖了onFaliure方法,这是当服务器没有响应时我得到的:
E/statusCode(31980): 0
E/headers(31980): null
E/Throwable(31980): org.apache.http.conn.ConnectTimeoutException: Connect to .. timed out
E/errorResponse(31980): null
异常也会打印在logcat上:
04-16 10:58:18.795: W/System.err(31980): org.apache.http.conn.ConnectTimeoutException: Connect to .. timed out
04-16 10:58:18.795: W/System.err(31980): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
04-16 10:58:18.795: W/System.err(31980): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-16 10:58:18.795: W/System.err(31980): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-16 10:58:18.795: W/System.err(31980): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-16 10:58:18.795: W/System.err(31980): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-16 10:58:18.795: W/System.err(31980): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
04-16 10:58:18.800: W/System.err(31980): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
04-16 10:58:18.800: W/System.err(31980): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
04-16 10:58:18.800: W/System.err(31980): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
04-16 10:58:18.800: W/System.err(31980): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
04-16 10:58:18.800: W/System.err(31980): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
04-16 10:58:18.800: W/System.err(31980): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-16 10:58:18.800: W/System.err(31980): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-16 10:58:18.800: W/System.err(31980): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-16 10:58:18.800: W/System.err(31980): at java.lang.Thread.run(Thread.java:841)
04-16 10:58:18.800: V/JsonHttpResponseHandler(31980): response body is null, calling onFailure(Throwable, JSONObject)
如果能提供一些解决方案,我将不胜感激。
试试这个:
AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(60000);
这将设置请求超时,然后您就有了所需的时间。
此错误是在1.4.4中引入的;
请尝试使用此库:https://github.com/leonardoxh/AsyncOkHttpClient
这个库使用OkHttp(来自Square股份有限公司)而不是Apache Http客户端,也是2.0 Loopj库的代码
;)
一年多后,这个错误不时出现。非常有趣的是,尽管stacktrace说
"响应主体为null,正在调用onFailure(Throwable,JSONObject)"
实际调用的方法是onFailure(statusCode, headers, throwable, (JSONObject) null);
根据第231行JsonHttpResponseHandler.java,这表明具有以下签名的方法是要重写的方法:
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse)