简体   繁体   English

在 Android 上将 div 加载到 webview

[英]Load a div into a webview on Android

I have an Android app, which contains a WebView, and I would like to display in it not a webpage, but only a div from that webpage.我有一个 Android 应用程序,其中包含一个 WebView,我想在其中显示的不是网页,而是该网页中的一个 div。 I should mention that I do not have access to that page.我应该提到我无权访问该页面。

I would recommend Jsoup .我会推荐Jsoup It's larger than tagsoup but provides inbuilt functionality to pull the HTML from the URL and is super easy to use.它比 tagsoup 大,但提供了从 URL 中提取 HTML 的内置功能,并且非常易于使用。 For example if you want to pull a div with id example you would do the following:例如,如果你想拉取一个带有 id examplediv ,你将执行以下操作:

Document doc = Jsoup.connect(url).get();
Elements ele = doc.select("div#example");

Then to load the HTML you've extracted into your web view you would do:然后将您提取的 HTML 加载到您的 Web 视图中,您可以执行以下操作:

String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
webView.loadData(html, mime, encoding);

You'll need to load the HTML of the page yourself, extract the div contents and pass it to the WebView as a string.您需要自己加载页面的 HTML,提取 div 内容并将其作为字符串传递给 WebView。 You may find an HTML parser library (eg tagsoup ) useful for this.您可能会发现一个 HTML 解析器库(例如tagsoup )对此很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM