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

13

34

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?

Featurelength answered 23/8, 2017 at 11:43 Comment(8)
You can't. It's a server error.Elastin
Is there any alternative solution?Featurelength
Try using OkHttp. It worked for me some times, but i don't guarantee.Elastin
@phaneendratatapudi how to solve this error ?Pahl
what if this occurs with localhost, I am using xampp and getting same error but when i use Postman it works just fine .. Android connection problem?Omnirange
With OkHttp also I am getting same error with my serverTessatessellate
it works fine using apche library but okhttp i am getting this error? i tried below solutions but no use.Defeasible
Users looking to solve the same issue from server may be ended here, so is there any suggestion on solving it from server side? In my case I am looking for solution on nodejs server.Daphene
P
46

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")
                             ...
Pushcart answered 3/10, 2018 at 7:59 Comment(8)
This is the answer. It works perfectly to me!!. I had been looking for hours.Eternize
I had a method with Request object passed as a parameter. Have to use the following code snippet instead >> request.addHeader("Connection", "close");Tightrope
It's perfect answer!Jago
Adding the Connection: Close header gave me another error (javax.net.ssl.SSLException: Write error: ssl=0x7b730be688: I/O error during system call, Broken pipe)Woven
Even after adding this, server randomly throws error as stated by OP.Leo
Quoting https://mcmap.net/q/435309/-java-io-ioexception-unexpected-end-of-stream-on-connection-in-android > Recommendation to close connections on the client side should be considered just as a workaround, keep in mind that it degrades overall performance.Complice
Wooooow! This answer saved our day! We use Apache NiFi for integration with TOTVS Protheus ERP. NiFi runs the InvokeHTTP process, and Protheus was returning this error message. So we added a property named "connection" with the content "close" on InvokeHTTP processor attributes, and it worked like a charm! Thank you so much!Sarinasarine
yeap, 2024 okhttp 4.12.0, saved my day with this answerAddington
B
12

"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.

Bolzano answered 3/10, 2019 at 11:4 Comment(2)
This sounded really promising, but I checked my headers and I have Connection: keep-alive and Transfer-Encoding: chunked. I'm still having this issue.Woven
you cant observe socket connection, nowadays, android studio network profiling only support okhttp and httpurlconnectionManzoni
F
11

I encountered this problem today. And it turns out that it is the server fault, as in the server threw 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

Fervid answered 21/10, 2017 at 3:53 Comment(1)
the server "threw"Gas
T
3

Just found out the solution
It is really a server side problem AND The solution is to send the content-length header
If you are using php just make your code like this

<?php
ob_start();
// the code - functions .. etc ... example:
$v = print_r($_POST,1);
$v .= "\n\r".print_r($_SERVER,1);
$file = 'file.txt';
file_put_contents($file,$v);
print $v;


// finally
$c = ob_get_contents();
$length = strlen($c);
header('Content-Length: '.$length);
header("Content-Type: text/plain");
//ob_end_flush(); // DID NOT WORK !!
ob_flush()
?>

The trick used here is to send the content-length header using the output buffer

Theodore answered 14/2, 2018 at 23:13 Comment(2)
It's a server side problem all right, but there's no evidence here that the server uses PHP.Nigrify
I see what you mean, sorry for any misunderstanding, i'll edit my answerTheodore
M
3

I had the same problem, turned out I still had the proxy configured on the emulator while didn't have Charles open anymore

Majoriemajority answered 19/11, 2018 at 15:24 Comment(0)
C
1

If you're using OkHttp with the Ktor client (eg with kotlin multiplatform), consider enabling OkHttp's retryOnConnectionFailure configuration parameter (it's likely enabled by default in other environments).

As documented, this enables the client to silently recover from:

Stale pooled connections. The ConnectionPool reuses sockets to decrease request latency, but these connections will occasionally time out.

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*

HttpClient(OkHttp) {
    engine {
        config {
            retryOnConnectionFailure(true)
        }
    }
}

h/t

Complice answered 30/4, 2021 at 22:57 Comment(2)
it seems to be on by defaultFacient
@Facient thanks – it seems Ktor is what was causing it to be disabled; answer updated.Complice
O
0

I was testing my App with localhost using XAMPP and this error was occurring, The problem was with the port i was using skype using 443 port i simply quit skype and error was resolved!

Omnirange answered 26/3, 2018 at 6:30 Comment(0)
O
0

Its a server error. It means somehow execution returns to your client without the server sending actual response header.

If you have control over server code, check that after processing the request, you explicitly send a response header with a response code. That way retrofit knows the request has been processed.

Ofris answered 16/5, 2020 at 9:30 Comment(0)
C
0

I have the same issue. This error is caused by the server-side only supports http2. You need to update JDK to a version that supports http2 (>=jdk9) to fix this issue.

Consols answered 17/11, 2020 at 3:0 Comment(0)
W
0

Add "Connection: keep-alive" to yor Rest Api endpoint

 @Headers({"Content-Type: application/json", "Accept: application/json", "Connection: keep-alive"})

This is if your endpoint is being called consecutively

Weinrich answered 23/8, 2022 at 6:48 Comment(0)
P
0

When I received the error "unexpected end of stream..." of Web3/Blockchain Node (Java, Spring Boot, Red Hat OpenShift, Hyperledger BESU):

@Component
public class BlockchainNetwork {

    @Value("${app.besu-network.host}")
    private String besuNetworkHost;

    public Web3j buildBesuNetwork() {
        return Web3j.build(new HttpService(besuNetworkHost));
    }
}

Both ways worked for me, with the same performance (response time), but the Singleton solution is better (because a best practice - pattern)!

Solution with Header "Connection:close":

@Component
public class BlockchainNetwork {

    @Value("${app.besu-network.host}")
    private String besuNetworkHost;

    public Web3j buildBesuNetwork() {
        HttpService httpService = new HttpService(besuNetworkHost);
        httpService.addHeader("Connection", "close");

        return Web3j.build(httpService);
    }
}

Solution with Singleton (pattern):

@Component
public final class BlockchainNetwork {

    @Value("${app.besu-network.host}")
    private String besuNetworkHost;

    private static Web3j web3jSingletonInstance;

    public Web3j buildBesuNetwork() {
        if (web3jSingletonInstance == null) {
            web3jSingletonInstance = Web3j.build(new HttpService(besuNetworkHost));
        }
        return web3jSingletonInstance;
    }
}
Pagano answered 27/5 at 23:35 Comment(0)
A
-1

This may be an old thread, as for me, check your internet connection (Wifi) there might be some restriction on accessing some of your endpoints.

I've fixed mine by using/connecting to my mobile data.

-cheers/happy codings

Acropetal answered 12/12, 2018 at 7:38 Comment(0)
P
-1

Most probably there are 2 things, happening at the same time.
First, the url contains a port which is not commonly used AND secondly, you are using a VPN or proxy that does not support that port.
Personally, I had the same problem. My server port was 45860 and I was using pSiphon anti-filter VPN.
In that condition my Postman reported "connection hang-up" only when server's relpy was an error with status codes bigger than 0. (it was fine when some text was returning from server with no error code)
Then I changed my web service port to 8080 on my server and, WOW, it worked! although psiphon vpn was connected.
Therefore, my suggestion is, if you can change the server port, so try it, or check if there is a proxy problem

Popele answered 25/9, 2020 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.