Android中的Loopj超时异常

时间:2014-04-16 05:34:04

标签: android exception timeout connection-timeout loopj

我遇到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)

如果可以展示一些解决方案,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

这个错误是在1.4.4上引入的;

请尝试使用此库:https://github.com/leonardoxh/AsyncOkHttpClient

这个库使用OkHttp(来自Square Inc.)代替Apache Http Client,也是2.0 Loopj Library的代码

答案 1 :(得分:1)

试试这个:

AsyncHttpClient client = new AsyncHttpClient();
client.setTimeout(60000);

这将设置请求超时,然后您将有时间。