Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the HTML code from loaded WebView [duplicate]

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 ?

like image 972
Lukap Avatar asked Oct 22 '13 13:10

Lukap


2 Answers

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>');");
        }
});
like image 107
nurisezgin Avatar answered Nov 14 '22 23:11

nurisezgin


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

like image 1
Antrromet Avatar answered Nov 14 '22 21:11

Antrromet