10

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.

schlingel
  • 8,560
  • 7
  • 34
  • 62
  • Does this still ocur turning off wifi optimization (Setttings > Wifi > Menu (three dots button) > Advanced > Wifi optimization OR modify the sleep policy)? If so, chances are the device is trying to re-init the wifi connection while you are making an http request and it is not ready. HTHs! – petey Sep 08 '13 at 17:53
  • Let's give it a try. So to put your suggestion in other words: If this EOFException occurs, I should just retry adding the request? – schlingel Sep 09 '13 at 12:53
  • Did try it out. Same result. The error is pretty easy to reproduce: Just pause the application and restart it. Even reinstalling it in eclipse does produce the error. But deinstall it before manually and reinstall it and everything's fine. – schlingel Sep 21 '13 at 08:03

2 Answers2

13

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

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;
  }   
schlingel
  • 8,560
  • 7
  • 34
  • 62
  • 4
    I'm using volley library. How to override createConnection? There is no such a function! – Dr.jacky Jul 02 '14 at 10:02
  • As I state above the piece of code: That's a piece of code in the OkHttp HttpStack class not volley itself! You have to write a HttpStack, there you can set the header accordingly. – schlingel Jul 02 '14 at 10:20
  • 3
    The easisest way arround would be to use OkHttp as stack. Or you modify the OkHttp example: https://gist.github.com/JakeWharton/5616899 – schlingel Jul 02 '14 at 10:33
  • 1
    See this to learn how to override Volley connection properties: http://stackoverflow.com/a/25887160/369317 – Denys Kniazhev-Support Ukraine Jul 17 '15 at 10:33
0

I had this issue for about an hour. If you are trying to connect to a virtual machine rest service you need to change the hosts file on the android emulator.

I was puzzled for so long because I had already done this. I don't think it keeps the hosts file between reboots of the emulator. Which is annoying but now I (and maybe you) know...

Yoker
  • 500
  • 4
  • 20