在android中不完整的http响应

时间:2014-02-17 13:28:41

标签: android

我正试图从办公室服务器获得回复。响应至少有50,000行。但我没有得到完整的答复。我不知道哪里出了问题。是否完整的数据没有下载或我的InputStream的任何其他问题?

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpcontext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(mUrl);
HttpResponse mResponse = httpClient.execute(httpPost, httpcontext);
InputStream mResponseStream = mResponse.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(mResponseStream));
String line = "";
while ((line = br.readLine()) != null) {
    line = line.replaceAll("[\r\n]", "");
    Log.v("Response", line);
}

必要回复:

02-17 18:32:14.000: D/Lib(4121): Starts
02-17 18:32:14.010: D/Lib(4121): 12080964|~|1370.00
02-17 18:32:14.010: D/Lib(4121): 12083142|~|500.00
02-17 18:32:14.020: D/Lib(4121): 13051372|~|100.00
02-17 18:32:14.020: D/Lib(4121): 13040457|~|12.00
02-17 18:32:14.060: D/Lib(4121): 12010037|~|170.00
02-17 18:32:14.060: D/Lib(4121): .........
02-17 18:32:14.060: D/Lib(4121): Ends

获得回应:

02-17 18:32:14.000: D/Lib(4121): Starts
02-17 18:32:14.010: D/Lib(4121): 12080964|~|1370.00
02-17 18:32:14.010: D/Lib(4121): 12083142|~|500.00
02-17 18:32:14.020: D/Lib(4121): 13051372|~|100.00
02-17 18:32:14.020: D/Lib(4121): 13040457|~|12.00
02-17 18:32:14.060: D/Lib(4121): 12010037|~|170.00
02-17 18:32:14.060: D/Lib(4121): .........

我的回复始终以Starts关键字开头,以Ends关键字结尾。我总是得不到Ends个关键字。响应在25,000到33,000行内停止。谢谢。

1 个答案:

答案 0 :(得分:0)

这里我从响应中获取字符串,之后我将该字符串响应转换为InputStream,此过程提供完整的响应。当我尝试从响应中获取InputStream时,它会返回不完整的数据。无论如何,我得到了完整的答复,但直到我不知道为什么我无法使用mResponse.getEntity().getContent()得到完整的回复。

我的工作代码,

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpcontext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(mUrl);
HttpResponse mResponse = httpClient.execute(httpPost, httpcontext);
String responseString=EntityUtils.toString(mResponse.getEntity());
InputStream mResponseStream = new ByteArrayInputStream(responseString.getBytes(HTTP.UTF_8));
BufferedReader br = new BufferedReader(new InputStreamReader(mResponseStream));
String line = "";
while ((line = br.readLine()) != null) {
    line = line.replaceAll("[\r\n]", "");
    Log.v("Response", line);
}