问题描述
我在写我与谷歌凌空GSON应用Talk与OkHttp REST服务的HTTP协议栈。这工作好大部分的时间,但是当我停下我的应用程序,并返回到它的HTTP请求不同意这种异常工作:
I'm writing my app with Google Volley and Gson to talk to a REST service with OkHttp as HTTP-Stack. That works good most of the time but when I pause my app and return to it the HTTP requests don't work with this Exception:
09-08 19:29:19.611: E/ASDF(21827): com.android.volley.NoConnectionError: java.io.EOFException
09-08 19:29:19.611: E/ASDF(21827): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:125)
09-08 19:29:19.611: E/ASDF(21827): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
09-08 19:29:19.611: E/ASDF(21827): Caused by: java.io.EOFException
09-08 19:29:19.611: E/ASDF(21827): at java.util.zip.GZIPInputStream.readFully(GZIPInputStream.java:206)
09-08 19:29:19.611: E/ASDF(21827): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:98)
09-08 19:29:19.611: E/ASDF(21827): at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:81)
09-08 19:29:19.611: E/ASDF(21827): at com.squareup.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:461)
09-08 19:29:19.611: E/ASDF(21827): at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:659)
09-08 19:29:19.611: E/ASDF(21827): at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:346)
09-08 19:29:19.611: E/ASDF(21827): at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:295)
09-08 19:29:19.611: E/ASDF(21827): at com.squareup.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:489)
09-08 19:29:19.611: E/ASDF(21827): at com.squareup.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:136)
09-08 19:29:19.611: E/ASDF(21827): at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:109)
09-08 19:29:19.611: E/ASDF(21827): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
09-08 19:29:19.611: E/ASDF(21827): ... 1 more
这是随机发生的。不是每次我暂停我的申请。我真的不知道从哪里开始。
That happens randomly. Not everytime I pause my application. I really have no idea where to start.
推荐答案
看来,这个问题是由Android的本身就是一种错误,这应该是固定的!?所致这个问题和它的修复在这里descriped:
It seems that this issue is caused by an bug in Android itself which should be fixed!? The issue and its fix is descriped here: Android issue 24672
所以加入这块code到我OkHttp URLConnection的工厂解决了该问题的时候了:
So adding this piece of code to my OkHttp URLConnection factory fixed the issue right away:
@Override protected HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection connection = client.open(url);
// Fix for bug in Android runtime(!!!):
// https://code.google.com/p/android/issues/detail?id=24672
connection.setRequestProperty("Accept-Encoding", "");
return connection;
}
这篇关于com.android.volley.NoConnectionError暂停应用程序后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!