Search code examples
androidapacherequestresponse

Android HTTPS con.getResponseCode() throws EndOfFile exception on readUtf8LineStrict in big requests


THE PROBLEM

I have a post request is causing an error only when data to recover are long (about 60Mb of data in json format).

The same request with less information does not cause any errors and it works perfectly.

The error message that is always appearing at exactly 2 minutes haver made the request, is the folowing:

java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:98)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:202)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:119)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:798)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
at com.myproject.mypackage.DataManagerService$ServiceHandler.sincronitzar_baixada_dades(DataManagerService.java:1406)
at com.myproject.mypackage.DataManagerService$ServiceHandler.handleMessage(DataManagerService.java:403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)

On the server side, we found that the service is preparing the data still alive after the error occurs on my device.

A few days ago we increased the RAM on the server to avoid OOM errors in the same casuistry.

MORE CODE UPDATED 03/06/2016

also attached the code that makes the request and saves the response in a file on the device:

The request code:

HttpsURLConnection con = (HttpsURLConnection) Https_url.openConnection();
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
                con.setRequestProperty("Connection", "close");
            }
            con.setReadTimeout(DEFAULT_RESPONSE_TIMEOUT);
            con.setConnectTimeout(DEFAULT_RESPONSE_TIMEOUT);
            con.setRequestMethod("POST");
            con.setRequestProperty("Accept-Encoding","identity");
            con.setDoInput(true);
            con.setDoOutput(true);
            con.connect();
            int status = con.getResponseCode(); 
            if (status >= 400) {
                inputStreamPujada = con.getErrorStream();
            }
            else{
                inputStreamPujada = con.getInputStream();
            }

            if (inputStreamPujada != null){
                try {
                    OutputStream out = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int len;
                    while((len=in.read(buf))>0){ <--// THIS LINE IS THROWING EOFExceptiono error!
                        out.write(buf,0,len);
                    }
                    out.close();
                    in.close();
                }
                catch (Exception e) { e.printStackTrace(); }
            }

WHAT WE TRYED?

  • We tryed to format json outputs with no breaklines and also with pretty json format.
  • It seems that error is caused because the DataStream dosen't found an "\n" and throws an EndOfFile Exception
  • Also tryed to raise up the memory RAM in the server side with no more effect.
  • Also tryed edit the request property "Accept-Encoding" as this question

THE QUESTION

What is the cause of this EOFException? Why is working with low data and throwing this in big data? Ther are some extrange timeout or library limit with max time in 2 minutes?

Thanks in advance!


Solution

  • After much research, we modify the server side to send the volume of data in a paged system of 5000 records for every flush of the JSON file.

    It seems that the server was blocked by addressing data and that made the request ends with a EOFException as shown in the question.

    I hope this question will be useful to someone else someday. Thank you!