简体   繁体   English

如何从 WebView 中提取 html 代码并将其放入 TextView?

[英]How to extract html code from WebView and put it in TextView?

I want to extract the html code from WebView and put it in TextView.我想从 WebView 中提取 html 代码并将其放入 TextView。 I search it in internet and I found this code.我在互联网上搜索它,我找到了这段代码。

    /* An instance of this class will be registered as a JavaScript interface */ 
    class MyJavaScriptInterface 
    { 
            private TextView textview1;

            public MyJavaScriptInterface(TextView textview1)
            {
                    textview1 = textview1;
                }

            @SuppressWarnings("unused") 

            public void processContent(String aContent) 
            { 
                    final String content = aContent;
                    textview1.post(new Runnable() 
                    {    
                            public void run() 
                            {          
                                    textview1.setText(content);        
                                }     
                        });
                } 
        } 

    

       webview1.getSettings()
      .setJavaScriptEnabled(true); 
   
    webview1.addJavascriptInterface
  (new MyJavaScriptInterface
 (textview1), "INTERFACE"); 
    
webview1.loadUrl
("http://blog.depauptits.nl/?m=1");
webview1.setWebViewClient(new
WebViewClient() 



{ 
@Override public boolean ...
 shouldOverrideUrlLoading(WebView 
 view, String url) 
{
     return true; } 
@Override public void onPageStarted
(WebView view, String url, Bitmap favicon) 
{ } public void 
onPageFinished(WebView view, String url) {
    
    
     
 webview1.loadUrl("javascript:window
.LOADHTML.processHTML
('<html>'+document
.getElementsByTagName('html') 
 [0].innerHTML+'</html>');"); } });

The code is working.代码正在运行。 The app is running the WebView load its URL, but the TextView doesn't display the html code.该应用程序正在运行 WebView 加载其 URL,但 TextView 不显示 html 代码。 I'm asking your help please correct my code.我正在请求您的帮助,请更正我的代码。

you can use jsoup dependency for fetch html from url您可以使用 jsoup 依赖项从 url 获取 html

this is the implementation of build.gradle :-这是 build.gradle 的实现:-

 implementation 'org.jsoup:jsoup:1.11.3'
String url = "http://www.google.com";
Document document = Jsoup.connect(url).get();

by this way you can get the document object that hold the full html along with ids and tags and all.通过这种方式,您可以获得包含完整 html 以及 ID 和标签以及所有内容的文档对象。

for more you can refer :有关更多信息,您可以参考:

https://jsoup.org/ https://jsoup.org/

This is the tutorial you can refer :这是您可以参考的教程:

https://www.tutorialspoint.com/jsoup/jsoup_load_url.htm https://www.tutorialspoint.com/jsoup/jsoup_load_url.htm

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

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