How to retrieve HTML content from WebView (as a string) [duplicate]
How do I retrieve all HTML content currently displayed in a WebView?
I found WebView.loadData()
but I couldn't find the opposite equivalent (e.g. WebView.getData())
Please note that I am interested in retrievi开发者_运维技巧ng that data for web pages that I have no control over (i.e. I cannot inject a Javascript function into those pages, so that that it would call a Javascript interface in WebView).
webView.evaluateJavascript("(function(){return window.document.body.outerHTML})();",
new ValueCallback<String>() {
@Override
public void onReceiveValue(String html) {
}
});
Add this to your code:
private String getUrlSource(String site) throws IOException {
//GNU Public, from ZunoZap Web Browser
URL url = new URL(site);
URLConnection urlc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
then lets say you what to get Google's source you would do:
getURLSource("http://google.com");
You can intercept the HTTP requests made by the WebView, and then modify the HTML to include whatever JavaScript functions you need to communicate with the HTML page. You intercept HTTP requests via the WebViewClient shouldInterceptRequest() method.
Using this mechanism you can get access to the loaded page by loading it yourself, modify it before passing it on to the WebView, and even cache it locally if you want.
you can pass data via JavaScriptInterface from webview.. i've done this. save the data to an static variable then process under android applcation
精彩评论