我想在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/