所以我有一个服务器和一个客户端。
服务器发送如下
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有问题,因为字符串有问题,因为字符串太长了?
谢谢你的帮助。