java.io.IOException:android 中连接上的流意外结束 [英] java.io.IOException: unexpected end of stream on Connection in android

查看:75
本文介绍了java.io.IOException:android 中连接上的流意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有网络服务 URL,它工作正常.它提供了 JSON 数据.

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

当我使用 HttpURLConnectionInputStream 时,我收到此错误:

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)

我的代码:

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

如何解决这个问题?

推荐答案

我在使用 OKHttp3 时遇到了同样的问题.问题是我没有关闭每个请求的连接,并且对于客户端,相同的连接可用,而对于服务器则没有,因此服务器返回错误.

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.

解决方案是指示每个请求在完成时关闭连接.您必须在标题中添加一个标志来表明这一点.在OKHttp3中是这样的:

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

这篇关于java.io.IOException:android 中连接上的流意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆