如何从 Android 中的 HTML 链接获取页面的 HTML 源代码? [英] How to get the HTML source of a page from a HTML link in Android?

查看:42
本文介绍了如何从 Android 中的 HTML 链接获取页面的 HTML 源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序需要从链接获取网页源,然后从该页面解析 html.

I'm working on an application that needs to get the source of a web page from a link, and then parse the html from that page.

你能给我一些例子,或者从哪里开始编写这样一个应用程序的起点?

Could you give me some examples, or starting points where to look to start writing such an app?

推荐答案

您可以使用 HttpClient 执行 HTTP GET 并检索 HTML 响应,如下所示:

You can use HttpClient to perform an HTTP GET and retrieve the HTML response, something like this:

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();

这篇关于如何从 Android 中的 HTML 链接获取页面的 HTML 源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆