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 ?

Lukap
  • 31,523
  • 64
  • 157
  • 244
  • I tried that solution already , but it does not work for me, maybe it is problem with android versions... – Lukap Oct 22 '13 at 13:31
  • Considering that you just accepted an answer which is the same as the duplicate, I find your response surprising. – CommonsWare Oct 22 '13 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 '13 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). – CommonsWare Oct 22 '13 at 14:16

2 Answers2

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>');");
        }
});
nurisezgin
  • 1,530
  • 12
  • 19
1

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

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75