10

I have something like this

final WebView w=(WebView)findViewById(R.id.webView1);
w.loadUrl("http://somepage.com");

Is there any way to get the html page that is shown in the WebView at some moment of time ? I want to get this html page as string variable.

The point is I want to get the html code after the javascript is executed on a client side...

any guidelines ?

4
  • I tried that solution already , but it does not work for me, maybe it is problem with android versions...
    – Lukap
    Oct 22, 2013 at 13:31
  • Considering that you just accepted an answer which is the same as the duplicate, I find your response surprising. Oct 22, 2013 at 13:43
  • well people say that this is stupid question and they asked for close so..., I still can not read the html text from the webview, but I guess this is problem only on my phone, cause everybody else say that is working fine , that is why I marked as answered.
    – Lukap
    Oct 22, 2013 at 14:14
  • 1
    Bear in mind that if your app has android:targetVersionSdk set to 17 or higher, you need to add the @JavascriptInterface annotation to any methods you wish to expose to JavaScript via addJavascriptInterface() (e.g., the processHTML() method in the answer that you accepted here). Oct 22, 2013 at 14:16

2 Answers 2

19

One way I know;

decleration javascript handler in your activity

class LoadListener{
    public void processHTML(String html)
    {
        Log.e("result",html);
    }
}

after configure your webview;

webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new LoadListener(), "HTMLOUT");

than webview client;

    webView.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) {
            view.loadUrl("javascript:window.HTMLOUT.processHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
        }
});
8
  • 5
    For API levels higher than 17, you need to add @JavascriptInterface annotation for that to work, otherwise it will not be called.
    – Tomap
    Aug 5, 2014 at 8:25
  • 1
    does this work in API 21 and Adroid 4.2.2? Doesn't seem to be working for me Android 4.2.2 with API 21. But it does work in android 4.4.2 and API 21.
    – user677607
    Dec 3, 2014 at 22:30
  • I tried the code. After view.loadUrl() in onPageFinished() is done, onPageFinished() is not called. How could we know the inner loadUrl() is done? Aug 18, 2015 at 19:56
  • Is your url wait for request string? Maybe Same params? Could you post your request address?
    – nurisezgin
    Aug 19, 2015 at 21:12
  • "Uncaught TypeError: window.HTMLOUT.processHTML is not a function", source: (1) Feb 4, 2017 at 12:00
1

One way to get the code is by using the HttpClient as given here. Another solution is given in the following blog.

1
  • 1
    This will not bring along cookies etc. May 29, 2015 at 3:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.