How I get page source from WebView?

后端 未结 3 2000
[愿得一人]
[愿得一人] 2020-12-08 12:21

How I get the web page\'s source from WebView?

I want to only enter www.google.com in my webview and When I entered this site, I want to get the source for example <

3条回答
  •  -上瘾入骨i
    2020-12-08 12:58

    I am not sure how far this is going to be helpful. But I have used the below snippet to fetch a small html page's data. I hope it helps you.

    Create a class like the one below,

      class MyJavaScriptInterface
      {
          @SuppressWarnings("unused")
          public void processHTML(final String html)
          {
              Log.i("processed html",html);
    
                Thread OauthFetcher=new Thread(new Runnable() { 
    
                    @Override
                    public void run() {
    
                        String oAuthDetails=null;
                          oAuthDetails=Html.fromHtml(html).toString();
                          Log.i("oAuthDetails",oAuthDetails);
    
                    }
                });OauthFetcher.start();
            }
          } 
    

    Now in your onCreate(),

     webview.getSettings().setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
    
         webview.setWebViewClient(new WebViewClient(){
    
                @Override
                public void onPageFinished(WebView view, final String url) {
    
    
                    String oAuthUrl=getString("www.google.com");
    
                    if(url.contains(oAuthUrl))
                    {
                        Log.i("Contains","Auth URL");
    
                        twitter_webview.loadUrl("javascript:window.HTMLOUT.processHTML(''+document.getElementsByTagName('html')[0].innerHTML+'');");
                    }
                }
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
    
                    progressDialog.show();
                }
          });
    

    And now what happens is that, when your page finishes loading, the JavaScript class will be called, which would retrieve the page source and store it in a String as your requirement.

提交回复
热议问题