Loopj timeout exception in Android

北战南征 提交于 2019-12-25 02:48:21

问题


I am having a strange problem with Loopj Async HTTP library. I sometimes get the response using GET or POST, sometimes the response comes after a long time, some times the response does not come at all. This happens only when wifi is used. I have used loopj in so many projects and those work fine with wifi but not this project. When I use my mobile data 3G connection, always the response comes. I have overridden the onFaliure method and this is what I get when the response from server does not come:

E/statusCode(31980): 0
E/headers(31980): null
E/Throwable(31980): org.apache.http.conn.ConnectTimeoutException: Connect to .. timed out
E/errorResponse(31980): null

An Exception also gets printed on 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)

I would greatly appreciate if some solution can be shown.


回答1:


This bug was introduced on 1.4.4;

Try use this library instead: https://github.com/leonardoxh/AsyncOkHttpClient

This library uses OkHttp (from Square Inc.) instead of Apache Http Client, and also is the codes for 2.0 Loopj Library

;)




回答2:


More than an year later this error keeps popping up from time to time. Quite interestingly even though the stacktrace says

"response body is null, calling onFailure(Throwable, JSONObject)"

The actual method that's invoked is onFailure(statusCode, headers, throwable, (JSONObject) null);

This is as per line 231 JsonHttpResponseHandler.java this would suggest that the method with the following signature is the one to be overridden:

public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse)



回答3:


Try this:

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

That will set the request timeout and then you will have the time you need.



来源:https://stackoverflow.com/questions/23100803/loopj-timeout-exception-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!