android - 如何在 android 中获取网页的 html 代码?

标签 android

我想在android中获取网页的html代码。网页 url 将在编辑文本框中给出,然后当用户单击按钮时, TextView 将显示该网页的代码。请解释并给出代码!

如有任何帮助,我们将不胜感激!

最佳答案

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

不要忘记在 AndroidManifest 中添加互联网权限:

<uses-permission android:name="android.permission.INTERNET" /> 

您可以引用这些链接以获得更多帮助:

http://lexandera.com/2009/01/extracting-html-from-a-webview/

Is it possible to get the HTML code from WebView

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

关于android - 如何在 android 中获取网页的 html 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903482/

相关文章:

Android Studio 自动为 Extract String Resource 生成名称

android - 在 PagedListAdapter 中未调用 onCreateViewHolder

android - 在 Braintree 中存储和检索借记卡和信用卡详细信息

php - 在android中使用php登录

Android 应用程序启动图标大小

java - 从 Android 中调用 Firebase Cloud Function 接收数据

android - 如何在不使用 Lambda 表达式的情况下使用新的 Android 架构?

android - 预填充的数据库在 API 28 上不起作用,抛出 "no such table"异常

java - Crouton 库中的滚动文本

android - 如何在 android 中解析这个嵌套的 JSON 数组