Wednesday, February 29, 2012

How to get HTML content of URL open in Android Webview?

In OnCreate method copy the below code and the desire'd URL....


mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

progressDialog.ShowProgressDialog(0, getString(R.string.Loading_data));

mWebView.setWebViewClient(new WebViewClient() {
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebView.loadUrl(url);
return false;
   }
 
   @Override
   public void onPageStarted(WebView view, String url, Bitmap favicon){
   }

   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
progressDialog.CloseProgressDialog();
   }

   @Override
   public void onPageFinished(WebView view, String url) {
progressDialog.CloseProgressDialog();
mWebView.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");

   }
});
mWebView.loadUrl(selectedSocial);

And below is the method which we have registered will give the html content in String.......


/* An instance of this class will be registered as a JavaScript interface */
    class MyJavaScriptInterface {
public void showHTML(String html) {
    String htmlString = html;
             }
}


2 comments: