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

后端 未结 10 1027
你的背包
你的背包 2020-12-01 12:40

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

When I am using HttpURLConnection and InputStream, I am getting this erro

相关标签:
10条回答
  • 2020-12-01 13:08

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

    0 讨论(0)
  • 2020-12-01 13:09

    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

    0 讨论(0)
  • 2020-12-01 13:09

    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.

    0 讨论(0)
  • 2020-12-01 13:15

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

    0 讨论(0)
  • 2020-12-01 13:17

    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

    0 讨论(0)
  • 2020-12-01 13:20

    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")
                                 ...
    
    0 讨论(0)
提交回复
热议问题