How to Get the HTML Code from Webview

Get the HTML code from loaded WebView

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>');");
}
});

How to retrieve HTML content from WebView (as a string)

Unfortunately there is not easy way to do this.

See How do I get the web page contents from a WebView?

You could just make a HttpRequest to the same page as your WebView and get the response.

How to access HTML source from Android WebView?

I found my quesntion's solution in this post.

https://stackoverflow.com/questions/6503574/how-to-get-html-source-code-from-url-in-android[][1]

I don't know much about Ion dependency but it did my work.

Is it possible to get the HTML code from WebView

Had to use HttpClient. no cookies required, just parsing for html:

private String getDownloadButtonOnly(String url){
HttpGet pageGet = new HttpGet(url);

ResponseHandler<String> handler = new ResponseHandler<String>() {
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
HttpEntity entity = response.getEntity();
String html;

if (entity != null) {
html = EntityUtils.toString(entity);
return html;
} else {
return null;
}
}
};

pageHTML = null;
try {
while (pageHTML==null){
pageHTML = client.execute(pageGet, handler);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Pattern pattern = Pattern.compile("<h2>Direct Down.+?</h2>(</div>)*(.+?)<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(pageHTML);
String displayHTML = null;
while(matcher.find()){
displayHTML = matcher.group();
}

return displayHTML;
}

@Override
public void customizeWebView(final ServiceCommunicableActivity activity, final WebView webview, final SearchResult mRom) {
mRom.setFileSize(getFileSize(mRom.getURLSuffix()));
webview.getSettings().setJavaScriptEnabled(true);
WebViewClient anchorWebViewClient = new WebViewClient()
{

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
String downloadButtonHTML = getDownloadButtonOnly(url);
if(downloadButtonHTML!=null && !url.equals(lastLoadedURL)){
lastLoadedURL = url;
webview.loadDataWithBaseURL(url, downloadButtonHTML, null, "utf-8", url);
}
}

How to get the HTML code from a webview?

If you don't understand javascript interfaces:

  • http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)
  • http://developer.android.com/guide/webapps/webview.html#BindingJavaScript


Related Topics



Leave a reply



Submit