gpt4 book ai didi

java - BufferedReader 由 HTTPClient 终止

转载 作者:行者123 更新时间:2023-12-01 14:46:48 26 4
gpt4 key购买 nike

所以我有一个服务器和一个客户端。

服务器发送如下

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException        
StringBuilder builder = new StringBuilder();
resp.setContentType("text/plain");

DO A LOT OF THINGS AND WRITE IT INTO ONE JSONARRAY

builder.append(JsonArray.toString());
resp.getWriter().println(builder);
}

客户端收到如下:

 private class GetXMLTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = getOutputFromUrl(url);
}
return output;
}

private String getOutputFromUrl(String url) {
String output = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();

InputStream in = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line = "";
StringBuilder builder = new StringBuilder();

while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}
Log.i("Test", builder.toString());
in.close();

httpClient.getConnectionManager().shutdown();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return output;
}

客户端从异步任务获取数据。

问题是客户端没有收到服务器发送的全部内容。我尝试制作一个 reader.read(),但也存在同样的问题。

如果我尝试通过浏览器获取代码,我会从服务器接收所有数据,但在我的 java 应用程序中却不会。所以服务器是完全正确的,也许我可以更改 jsonarray 并在每个 jsonobject 之后添加一个\n 。

logcat 是否有问题,因为它的字符串有问题,因为字符串太长?

感谢您的帮助。

编辑:关闭 After HTTP GET request, the resulting string is cut-off - content has been consumed

最佳答案

在方法“getOutputFromUrl”中,没有任何内容分配给“output”

关于java - BufferedReader 由 HTTPClient 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15364696/

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