我在我的项目中使用android-async-http,而AsyncHttpClient是静态单实例,我的代码如下:
06-28 18:35:36.440 18638-19939 / com.zebra.carcloud.example W / System.err:cz.msebera.android.httpclient.conn.ConnectTimeoutException:connect to daily.yuncar.zebred.com/ 139.196.2.83:7090超时 06-28 18:35:36.441 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 06-28 18:35:36.441 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:157) 06-28 18:35:36.441 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.conn.scheme.SchemeSocketFactoryAdaptor.connectSocket(SchemeSocketFactoryAdaptor.java:65) 06-28 18:35:36.441 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177) 06-28 18:35:36.441 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:145) 06-28 18:35:36.442 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:131) 06-28 18:35:36.442 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611) 06-28 18:35:36.442 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446) 06-28 18:35:36.442 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860) 06-28 18:35:36.442 18638-19939 / com.zebra.carcloud.example W / System.err:at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) 06-28 18:35:36.442 18638-19939 / com.zebra.carcloud.example W / System.err:at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146) 06-28 18:35:36.443 18638-19939 / com.zebra.carcloud.example W / System.err:at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177) 06-28 18:35:36.443 18638-19939 / com.zebra.carcloud.example W / System.err:at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106) 06-28 18:35:36.443 18638-19939 / com.zebra.carcloud.example W / System.err:at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:423) 06-28 18:35:36.443 18638-19939 / com.zebra.carcloud.example W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237) 06-28 18:35:36.443 18638-19939 / com.zebra.carcloud.example W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 06-28 18:35:36.443 18638-19939 / com.zebra.carcloud.example W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:588) 06-28 18:35:36.444 18638-19939 / com.zebra.carcloud.example W / System.err:at java.lang.Thread.run(Thread.java:818)
我谷歌一段时间但无法找到原因,有人遇到同样的问题:loopj issues 725,但我将AsyncHttpClient超时设置为30000(默认为10000),并在30秒后仍然获得ConnectTimeoutException
在第二次请求之前将单个实例修改为新的AsyncHttpClient实例,每件事都可以。我不知道HttpURLConnection操作是否会影响AsyncHttpClient。