Android中的Loopj超时异常 [英] Loopj timeout exception in Android

查看:250
本文介绍了Android中的Loopj超时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Loopj Async HTTP库有一个奇怪的问题。我有时会使用GET或POST获取响应,有时响应长时间后,有时候响应不会出现。这仅在使用wifi时才会发生。我在这么多项目中使用了loopj,而这些项目的工作正常,而不是这个项目。当我使用我的移动数据3G连接时,始终会有回应。我已经覆盖了onFaliure方法,这是当服务器的响应不到时,我得到的:

  E / statusCode(31980 ):0 
E / headers(31980):null
E / Throwable(31980):org.apache.http.conn.ConnectTimeoutException:连接到..超时
E / errorResponse 31980):null

异常也会打印在logcat上:

  04-16 10:58:18.795:W / System.err(31980):org.apache.http.conn.ConnectTimeoutException:连接到.. timed out 
04-16 10:58:18.795:W / System.err(31980):在org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
04-16 10 :58:18.795:W / System.err(31980):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
04-16 10:58:18.795:W / System.err(31980):在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-16 10:58:18.795:W / System。 err(31980):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-16 10:58:18.795:W / System.err(31980):在org .apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-16 10:58:18.795:W / System.err(31980):在org.apache.http.impl。 client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
04-16 10:58:18.800:W / System.err(31980):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient .java:509)
04-16 10:58:18.800:W / System.err(31980):在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):在java.util.concurrent.Executo rs $ RunnableAdapter.call(Executors.java:390)
04-16 10:58:18.800:W / System.err(31980):在java.util.concurrent.FutureTask.run(FutureTask.java:234 )
04-16 10:58:18.800:W / System.err(31980):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-16 10:58 :18.800:W / System.err(31980):在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573)
04-16 10:58:18.800:W / System.err 31980):在java.lang.Thread.run(Thread.java:841)
04-16 10:58:18.800:V / JsonHttpResponseHandler(31980):响应体为null,调用onFailure(Throwable,JSONObject)

如果可以显示某些解决方案,我将非常感激。

解决方案

尝试这样:

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

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


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.

解决方案

Try this:

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

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

这篇关于Android中的Loopj超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆