如何强制HttpClient获取普通的HTML?

时间:2013-03-22 04:13:14

标签: android httpclient

我正在编写简单的Android应用程序,它在特定位置解析html,获取一些数据并显示在ListView中。由于app只需要普通的html响应,因此有可能只请求普通的html。如何从http请求中排除css和java脚本文件,图像和类似内容? 我正在使用apache HttpClient对象。

1 个答案:

答案 0 :(得分:1)

这是否回答了你的问题?如果是这样,请记住在单独的线程上执行此操作以避免UI阻塞(可能想要使用AsyncTask

(来自How to get the html-source of a page from a html link in android?

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);

String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
    str.append(line);
}
in.close();
html = str.toString();