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

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

问题描述

我有Web服务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中的Connection上的流意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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