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
Why Does My "Oninvalid" Attribute Let the Pattern Fail
Size of Font in CSS with Slash
What Is the Meaning of ? (Question Mark) in a Url String
Regular Expression to Extract Text from HTML
Preventing Firefox from Remembering the Input Value on Refresh with a Meta Tag
Dynamically Adding and Removing Components in Angular
Android: How to Use the HTML.Taghandler
CSS Float: Floating an Image to the Left of the Text
Should Global CSS Styles Be Set on the HTML Element or the Body Element
Send HTML Mail Using Android Intent
How to Determine Height of Content-Box of a Block and Inline Element
Bootstrap 3 to Bootstrap 4 Cols No Longer Horizontally Aligned
How to Make an Element's Background-Color a Little Darker Using CSS
How to Change the Length/Position of Text-Decoration:Underline
Should a Heading Be Inside or Outside a ≪P≫
<Input Type="Number"> Not Working in Ie10