如何显示到Android的网页视图之前清除html页面? [英] How to clear html page before showing into a webview in Android?

查看:136
本文介绍了如何显示到Android的网页视图之前清除html页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示到我的Andr​​oid应用程序网页视图网页的URL。显示此页之前,我想从一些标记清除此页面的HTML code(如页眉,页脚,ECC ..),以显示只有少数的信息。我该怎么办呢?我试图解决JSoup工作问题,但我不明白如何创造和传递的新页的网页视图。任何人都可以帮我吗?

I have the URL of a webpage to be displayed into a webview in my Android app. Before showing this page i want to clear the html code of this page from some tag (such as the header, footer, ecc..) in order to show only few information. How can i do it? I tried to solve the issue working with JSoup but i can't understand how to create and pass the "new page" to the webview. Anybody can help me?

修改

我打扫HTML code到jsoup库没用。然后,总是通过这些意思,我让头部和身体的内容,最后我展示通过这些线的清除页面:

I cleaned the html code useless through jsoup libraries. Then, always by mean of these, i get head and body content and finally i showing the "cleared" web page through these lines:

headURL = doc.select("head").outerHtml();
bodyURL = doc.select("body").outerHtml();

webview.loadData( "<html>"+headURL+bodyURL+"</html>" , "text/html", "charset=UTF-8");

webview.setWebViewClient(new DisPlayWebPageActivityClient());

该视图显示了新的一页,但不加载在头部指定的CSS文件(尚未触及)。谁又能说我为什么?

The view shows the new page but do not load css files specified in the head(that has not been touched). Who can say me why?

推荐答案

您可以获取你想显示为一个字符串,解析并删除任何你不想再加载这个字符串作为你的WebView数据的网页。

You can fetch the WebPage you want to display as a string, parse and remove whatever you don't want and then load this string as data in your webview.

是这样的:

String webContent = fetchPage(url);
String cleanedWebContent = cleanUp(webContent);
webView.loadData(cleanedWebContent, "text/html", "UTF-8");

当然,你将需要实施 fetchPage 清除,因为它们不是Android的方法

Of course, you will need to implement fetchPage and cleanUp as they are not Android methods

这篇关于如何显示到Android的网页视图之前清除html页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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