Android: Get webview HTML source of the current page

Source: Internet
Author: User

WebView does not provide a direct API and needs to be adapted with JavaScript.
This article attempts to summarize one of the simplest and most elegant codes.

1. Create a JavaScript interface class first:

class myjavascriptinterface {    @JavascriptInterface    @SuppressWarnings ("unused")     publicvoid  processhtml(String html) {        //  Handle HTML source here     }}

Note the bold function name processhtml, which is used in the 2nd step.
For a detailed description of the @JavascriptInterface , see: http://blog.csdn.net/z82367825/article/details/52187921

2. Register the JavaScript interface just now:

WebSettings settings =webview.getsettings (); //This method requires JavaScript to be enabledSettings.setjavascriptenabled (true); //register the interface class you just had with the JavaScript interface named htmlout Webview.addjavascriptinterface (NewMyjavascriptinterface (), "Htmlout"); //The webviewclient must be set before LoadurlWebview.setwebviewclient (Newwebviewclient () {@Override Public voidonpagefinished (WebView view, String URL) {//Here you can filter the URLWebview.loadurl ("javascript:Htmlout.processhtml(Document.documentElement.outerHTML); ");        }        }); //Start loading URLsWebview.loadurl (URL);


Source: Https://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview

Android: Get webview HTML source of the current page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.