

Getting the HTML source from an Android WebView
source link: http://blog.weinigel.se/2014/03/05/getting-html-source-from-android-webview.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Getting the HTML source from an Android WebView
Mar 5, 2014
On and off I’m working on writing an Atom/RSS feed reader for Android. The feed reader is using an Android WebView to display the contents of a feed and to be able to debug some issues I had I wanted to be able to view the HTML source for a page.
That’s should be simple I though, just do WebView.getData() and show it in a dialog. But no, there’s no method to get the HTML source from a WebView, so one has to jump through a lot of hoops to do that. Also the examples I found uses WebView.addJavaScriptInterface to do its job which is not such a good idea because addJavaScriptInterface has security problems and doesn’t even work on Android 2.3. And I do want my application to run on fairly old Android devices (I still use a Motorola Defy at work, I like the form factor and that it’s waterproof).
So after a bit of thinking I figured out an easier way of getting the source. There is a class called WebViewClient which among other things can be used to override what should happen when a follows a link in the WebView. This together with a bit of JavaScript can be used to get the source.
First, when initializing the WebView, tell it to use a custom WebViewClient:
mWebView = (WebView) mView.findViewById(R.id.web);
mWebView.setWebViewClient(new MyWebViewClient());
When asked to view the source, enable JavaScript for the WebView and then inject a bit of JavaScript which builds and follows an URL containing the HTML:
public void viewSource() {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(
"javascript:this.document.location.href = 'source://' + encodeURI(document.documentElement.outerHTML);");
}
The custom WebViewClient will catch this URL:
public class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("source://")) {
try {
String html = URLDecoder.decode(url, "UTF-8").substring(9);
sourceReceived(html);
} catch (UnsupportedEncodingException e) {
Log.e("example", "failed to decode source", e);
}
mWebView.getSettings().setJavaScriptEnabled(false);
return true;
}
// For all other links, let the WebView do it's normal thing
return false;
}
}
And we can finally show the source in a dialog:
private void sourceReceived(String html) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(html);
builder.setTitle("View Source");
AlertDialog dialog = builder.create();
dialog.show();
}
As simple as that. And it seems to work on everything from Android 2.3 up to Android 4.4.
Although what we get from the WebView does not quite seem to be the source code it was fed to begin with. It seems that the WebView will fix up the HTML so that it is correct and will also decode any entitydefs. So for example if the WebView was fed “<p>Hello Wörld<p>” what we would get back is “<html><head></head><body><p>Hello Wörld<p></body></html>”. It’s still useful for what I wanted to do though.
Recommend
-
174
关于作者 郭孝星,程序员,吉他手,主要从事Android平台基础架构方面的工作,欢迎交流技术方面的问题,可以去我的
-
99
-
382
App中大量Web页面的使用容易导致App内存占用巨大,存在内存泄露,崩溃率高等问题,WebView独立进程的使用是解决Android WebView相关问题的一个合理的方案。 为什么要采用WebView独立进程 Android WebView的问题 WebView导致的OOM问题 Android版本不同,采用了不同...
-
101
1、前言 无论是日常开发还是练习,相信网页显示是经常需要实现的业务场景,现在的应用一般有网页链接传入,都直接在自己的应用中显示,不会去跳转自带浏览器。今天,我们就来实现一个能满足基本网页浏览需求的页面。国际惯例附上源码。 2、效果图 从图中可以看到进...
-
110
前言 现在业务稍微大一点的公司,基本上都会引入android与H5交互的方式开发,或者是引入Hybrid框架,更有甚者直接全部采用Js开发成Web App形式,就是看中其开发成本更低(跨平台),更新风险更小的优势。目前移动端开发市场的遇冷,除了android初
-
165
前言 Android应用层的开发有几大模块,其中WebView是最重要的模块之一。网上能够搜索到的WebView资料可谓寥寥,Github上的开源项目也不是很多,更别提有一个现成封装好的WebView容器直接用于生产环境了。本文仅当记录在使用WebView实现
-
112
前言 在上文《如何设计一个优雅健壮的Android WebView?(上)》中,笔者分析了国内WebView的现状,以及在WebView开发过程中所遇到的一些坑。在踩坑的基础上,本文着重介绍WebView在开发过程中所需要注意的问题,这些问题大部分在网上找不到
-
81
本系列文章一共有两篇:主要来讲解 webview 和客户端的交互。 本篇为第一篇:Android 和 webview 的交互 后续一篇是:IOS 和 webview 的交互 如需获得最新的内容,可以关注微信公众号:前端小吉米 在移动时代 Web 的开发方式逐渐...
-
40
本文首发于微信公众号「玉刚说」 原文链接:Android Webview H5 秒开方案实现 前言 现在许多app都嵌入了H5页面, 然而WebView加载速度慢这个问题却一直影响着用户的体验, 所以本文就如何提高H5页面的加载速度展开讨论。 问题原因 首
-
9
在日常开发过程中,有时候会遇到需要在app中嵌入网页,此时使用WebView实现效果,但在默认情况下是无法点击图片查看大图的,更无法保存图片。本文将就这一系列问题的实现进行说明。 项目的知识点: 加载网页后如何捕捉网页中的图...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK