Android WebView get HTML source

This may not work on android 4.3 and later because of webview thread security enhancement

In android WebView is the heart when developing Hybrid application. During development of such apps we might need view the source code web page in webview. Here is a piece of code which do the need. The basic idea is install a javascript interface to webview to handle html callback, then call the javascript on webview invoke the method.

first of all setup webview for enabled javascript,assign a custom webclient.

 

Webclient is responsible for notifying page has loaded and calling the javascript

 

Javascript interface object for callback here

 

As you see the above interface just print the html to System.out


  • azzz

    good one

  • Matrix Sourcing

    this is not working for me, seems function “print” is not called….

    • inverse12

      add @JavascriptInterface to method

  • Danx

    Great job, thank you very much!.