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

后端 未结 10 1067
你的背包
你的背包 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: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.

提交回复
热议问题