暂停app后com.android.volley.NoConnectionError

时间:2013-09-08 17:38:49

标签: android android-volley okhttp

我正在使用Google Volley和Gson编写我的应用程序,以使用OkHttp作为HTTP-Stack与REST服务进行通信。大部分时间都可以正常工作,但是当我暂停我的应用程序并返回它时,HTTP请求无法使用此异常:

     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

随机发生。不是每次我暂停我的申请。我真的不知道从哪里开始。

2 个答案:

答案 0 :(得分:13)

似乎这个问题是由Android本身的一个错误导致的,应该修复!此处描述了该问题及其修复:Android issue 24672

因此,将这段代码添加到我的OkHttp URLConnection工厂立即解决了这个问题:

  @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;
  }   

答案 1 :(得分:0)

我有这个问题大约一个小时。如果您尝试连接到虚拟机休息服务,则需要更改android模拟器上的hosts文件。

我很困惑,因为我已经这样做了。我不认为它会在重新启动模拟器之间保持主机文件。这很烦人,但现在我(也许你)知道...