在做这篇文章之前,我做了很长时间的搜索,但我做了很多测试,但没有成功。
我的问题(简而言之): 3g数据连接,每次我执行获取请求时,第一次连接成功完成。
现在,如果设备进入待机状态几分钟,当我重复相同的获取请求时,它显然会因我在下面报告的错误而失败。一些评论:
错误明细
javax.net.ssl.SSLException: Read error: ssl=0x75cab460: I/O error during system call,
Connection reset by peer
at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:689)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Response body is null, calling onFailure(Throwable, JSONObject)
让我们看一些代码。
我有一个单独的类,我管理所有连接:
...
//Static istance of my httpClient
private static AsyncHttpClient client = new AsyncHttpClient();
//This is the only parameter that I'm setting for the client
client.setTimeout(30000);
...
//When i need to a get request I just use the following code.
client.get(mContext, url, params, responseHandler);
//For testing purpose, before doing any get request, I've done a clear of all requests,
//but this is not helping with my problem.
client.cancelRequests(mContext, true);
我现在迷路了:我不知道我是否在客户端上遗漏了一些配置参数,或者客户端是否试图重用错误的连接等等......
我是图书馆的新用户,所以我真的可能做错了。
我希望有人可以帮助我。
提前致谢。
答案 0 :(得分:0)
有时我会发现Android清除静态类中包含的对象(在你的情况下,那个单例)。可能是这个错误不是关于你的3g连接,而是关于内存管理。 因此,最好的方法是创建一个返回客户端的方法。
public static void getClient(){
if(MySingleton.client == null){
MySingleton.client = new AsyncHttpClient();
}
return client;
}
我在这里给你举了一个例子,你应该用你在单身中保存的所有对象来做。
当你的应用程序使用大量内存并使用单例时,Android很棘手,即使你扩展了Application类,它也可以在Application单例中垃圾收集对象。