如何将大型网页加载到字符串中

时间:2011-09-04 11:40:23

标签: android httpclient

我是Java和Android的新手,但不是编程和HTTP。这个HTTP GET方法主要是使用Apache HTTP类从其他示例复制而来,只检索大型网页的前几个K.我检查过网页没有超过8192字节的行(这可能吗?),但是在40K左右的网页中我回来的可能是6K,也许是20K。读取的字节数似乎与总网页大小,网页模数8192或网页内容没有简单的关系。

任何想法的人?

谢谢!

public static String myHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
    HttpClient client = getHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    HttpResponse response = client.execute(request);
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer sbuffer = new StringBuffer("");
    String line = "";

    while ((line = in.readLine()) != null) {
        sbuffer.append(line + "\n");
    }
    in.close();

    String result = sbuffer.toString();
    return result; 
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

2 个答案:

答案 0 :(得分:2)

无需编写自己的HttpEntity-to-String代码,请尝试使用EntityUtils

// this uses the charset the server encoded the entity in
String result = EntityUtils.toString(entity);

答案 1 :(得分:0)

看起来问题是来自某个网​​站的页面开始Goo ...我没有来自其他网站的大页面的这个问题。所以代码可能没问题。

相关问题