gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:41 25 4
gpt4 key购买 nike

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

当我使用 HttpURLConnectionInputStream 时,出现此错误:

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 时遇到了同样的问题。问题是我没有为每个请求关闭连接,对于客户端,相同的连接可用,而对于服务器则不可用,因此服务器返回错误。

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

Request request = new Request.Builder()
.url(URL)
.header("Connection", "close")
...

关于android - java.io.IOException : unexpected end of stream on Connection in android 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45838774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com