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: unexpected end of stream on

Connection{, proxy=DIRECT


cipherSuite=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 protocol=http/1.1}

(recycle count=0)

My code:

try {

URL url = new URL("" + 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) {



How to fix this?


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()


.header("Connection", "close")


< <上一篇