简体   繁体   中英

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. 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. I'm asking your help please correct my code.

you can use jsoup dependency for fetch html from url

this is the implementation of 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.

for more you can refer :

https://jsoup.org/

This is the tutorial you can refer :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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