Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.IOException: unexpected end of stream on Connection in android

I have web service URL, it working fine. It gives the JSON data.

When I am using HttpURLConnection and InputStream, I am getting this error:

java.io.IOException: unexpected end of stream on
Connection{comenius-api.sabacloud.com:443, proxy=DIRECT
hostAddress=12.130.57.1
cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1}
(recycle count=0)

My code:

try {
    URL url = new URL("https://comenius-api.sabacloud.com/v1/people/username=" + username + ":(internalWorkHistory)?type=internal&SabaCertificate=" + certificate);

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    InputStream ist = con.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(ist));

    while ((singleLine = reader.readLine()) != null) {
        data = data + singleLine;
        Log.e("Response", data);
    }

} catch (Exception e) {
    e.printStackTrace();
}

How to fix this?

like image 595
phaneendra tatapudi Avatar asked Aug 23 '17 11:08

phaneendra tatapudi


3 Answers

I had the same problem using OKHttp3. The problem was that I didn't close the connection for each request and for the client the same connection was available and for the server not, for this reason the server returns a error.

The solution is indicating to each request to close the connection when it is finished. You have to add a flag in the header to indicate this. In OKHttp3 is like this:

Request request = new Request.Builder()
                             .url(URL)
                             .header("Connection", "close")
                             ...
like image 171
Radost Avatar answered Nov 05 '22 03:11

Radost


I encountered this problem today. And it turns out that it is the server fault, as in the server throwed an error and shut down as it is parsing the request.

Check your backend, if it is not yours, notify the owner of that server

like image 38
grandia Avatar answered Nov 05 '22 01:11

grandia


"Keepalive makes it difficult for the client to determine where one response ends and the next response begins" 1

It seems that the issue is caused by a collision on reusing alive connection under 2 cases:

  1. server doesn't send Content-Length in response headers

  2. (streaming content case, so no Content-Length can be used) server doesn't use Chunked transfer encoding

So if you observed the exception, sniff http headers (e.g. at Android Studio Profiler tool). If you will see in response header both

  • "Connection: keep-alive"

and no

  • "Content-Length: ***" or "Transfer-Encoding: chunked" headers,

then this is the described above case.

Since it is totally server issue, the solution should be to calculate Content-Length and to put it to response header on server side, if it is possible (or to use Chunked transfer encoding).

Recommendation to close connections on the client side should be considered just as a workaround, keep in mind that it degrades overall performance.

like image 11
TiMoFeJ Avatar answered Nov 05 '22 01:11

TiMoFeJ