如何在WebView Android上显示网页的一部分

编程入门 行业动态 更新时间:2024-10-24 22:29:44
本文介绍了如何在WebView Android上显示网页的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从网页中提取一小部分并加载到webview中 我已经尝试按照链接中给出的解决方案进行操作,但这没有用

I am trying to extract small portion from webpage and load into webview I have tried following solution given in the link,But it did not work

显示部分webview android上的网页

使用getElementsByClass("darewod")提取数据

htmlDocument = Jsoup.connect(htmlPageUrl).get(); element = htmlDocument.getElementsByClass("darewod"); String html = element.toString(); String mime = "text/html"; String encoding = "utf-8";

我尝试了以下两种方法来加载到webview,但似乎不起作用,它只是在UI上打印HTML

I have tried the following two methods to load to webview but it seems not working,Its just printing HTML on UI

wv1.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); wv1.loadData(html, "text/html", null);

您能告诉我我是否在这里遗漏了什么吗?

Can you please tell me if i am missing anything here?

推荐答案

您正在加载HTML代码,但没有正确的结构(因此head中的所有定义都将丢失,就像CSS引用一样),并且在没有初始文档(或加载基本URL)的情况下,所有相对路径都被破坏了.

Your are loading your html code without the proper structure (so all definitions in head are lost, like CSS references) and without the initial document (or loading with base url) all relative paths are broken.

<div class="darewod"> <a title="Workout of the Day" href="/workouts/lower-abs-workout.html" rel="alternate"><img src="/images/grid/wod/2016/wod_nov8.jpg" alt="Workout of the Day"></a> </div>

您可以做什么:用选定的元素替换文档的主体,然后保留有关基础的结构和信息:

What you could do: replace the body of your document with your selected element, therby preserving the structure and information regarding base:

示例代码

WebView wv; Handler uiHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wv = (WebView)findViewById(R.id.webView); wv.setWebViewClient(new MyWebViewClient()); new BackgroundWorker().execute(); } // load links in WebView instead of default browser private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return false; } @RequiresApi(21) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { view.loadUrl(request.getUrl().toString()); return false; } } private class BackgroundWorker extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... arg0) { getDarewod(); return null; } public void getDarewod(){ try { Document htmlDocument = Jsoup.connect("darebee/").get(); Element element = htmlDocument.select("#gkHeaderMod > div.darewod").first(); // replace body with selected element htmlDocument.body().empty().append(element.toString()); final String html = htmlDocument.toString(); uiHandler.post(new Runnable() { @Override public void run() { wv.loadData(html, "text/html", "UTF-8"); } }); } catch (IOException e) { e.printStackTrace(); } } }

更多推荐

如何在WebView Android上显示网页的一部分

本文发布于:2023-11-22 20:10:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网页   如何在   WebView   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!