HttpURLConnection没有阅读整个内容

时间:2014-09-04 12:38:20

标签: java android httpurlconnection

我试图阅读一个网站,但奇怪的是它只返回了部分内容。它只是在部分的中间结束。

我尝试使用setChunkedStreamingMode方法,但它没有改变任何内容。

HttpURLConnection connection = ((HttpURLConnection) new URL(url).openConnection());
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
// I write some data...
String content = readInputStream(connection.getInputStream());

ReadInputStream方法:

private static String readInputStream(InputStream in) throws IOException {
int len = 0;
byte[] buffer = new byte[10000];
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
while((len = in.read(buffer)) != -1) {
    byteArray.write(buffer,0, len);
}
in.close();

return new String(byteArray.toByteArray());

}

是否存在某种数据限制?

1 个答案:

答案 0 :(得分:0)

HttpUrlConnection没有问题 试试这个:

StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) { 
       stringBuilder.append(line);
}
inputStream.close();