`

HTML5 Cache, Android WebView

 
阅读更多

So last post was about HTML5 and WebViews but it didn’t cover offline usage and especially caching.

HTML5 is cool. It can work offline once cached by the browser. The main mechanism for offline HTML5 is the so called Manifest. Basically it’s a file which lists entries (js files, pictures, css, etc.) to be cached by the browser.

 

To enable cache in Android’s WebView we must do at least the following:

webView.setWebChromeClient(new WebChromeClient() {
      @Override
      public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,
                   WebStorage.QuotaUpdater quotaUpdater)
      {
            quotaUpdater.updateQuota(spaceNeeded * 2);
      }
});
 
webView.getSettings().setDomStorageEnabled(true);
 
// Set cache size to 8 mb by default. should be more than enough
webView.getSettings().setAppCacheMaxSize(1024*1024*8);
 
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
webView.getSettings().setAppCachePath(appCachePath);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAppCacheEnabled(true);

 

This will work. Well, this will make your WebView support caching.. doesn’t mean it will actually work.

You can tune cache policies using the following code, even though it’s not required to make it work:

webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

 

Now when all the cache is setup we would like to see our HTML5 site working in pure offline mode.. but we don’t!

Why? well.. if it’s not working for you it probably means that the site you load up with the following call is actually redirecting you somewhere else:

 

webView.loadUrl("http://myHTML5app.com/app/");

If /app/ is resolving to a redirect – you are in trouble, sir.

The Cache mechanism will cache the actual RESULT of the redirect and sure it will map it to the url you are being REDIRECTED to, not the one you originally called. So once you are offline – the cache has no clue about the /app/ url and thus you are simply given the default android “can’t open page” replacement.

 

There are at least two ways to solve this issue..

 

Get rid of the redirect on the server

Re-redirect on android side

The first option is bad because you typically don’t want to mess with the working HTML5 site which is working on pure Browsers (iPhone, desktop, etc.).

To implement the second option I used the following code in the WebViewClient implementation:

@Override
public void onReceivedError(WebView view, int errorCode,
           String description, String failingUrl)
{
    // The magic redirect  
    if( "http://HTML5app.com/app/".equals(failingUrl) ) {
          // main.html is the place we are redirected to by the server if we are online
          mWebView.loadUrl("http://HTML5app.com/app/main.html");
          return;    
    }
    else if( "http://HTML5app.com/app/main.html".equals(failingUrl) ) {    
          // The cache failed – We don't have an offline version to show
 
          // This code removes the ugly android's "can't open page"
          // and simply shows a dialog stating we have no network
          view.loadData("", "text/html", "UTF-8");
          showDialog(DIALOG_NONETWORK);
    }
}

 

Good luck!

=======================================

对于UIWebView不支持html5 cache的问题,可以参考:

 

http://stackoverflow.com/questions/1540240/html5-cache-manifest-in-a-uiwebview

分享到:
评论

相关推荐

    Android WebView 缓存详解

    Android WebView 缓存详解 一. 两种缓存类型: 页面缓存:加载一个网页时的html、JS、CSS等页面或者资源数据,这些缓存资源是由于浏览器 的行为而产生,开发者只能通过配置HTTP响应头影响浏览器的行为才能间接地...

    完美解决客户端webview持有的页面缓存,不会立即释放的问题

    当我们对页面进行销毁的时候,其中webview持有的HTML页面还会继续存在,加入我们在HTML页面中做了一些监听手机晃动、声音…… 以及使用了js定时任务的情况下。 单纯的销毁我们的native页面并不能达到让页面中这些...

    Google Android SDK开发范例大全(第3版) 5/5

    特别是新增加了第11章来专门介绍HTML5技术在Android移动设备里的应用,相信当下两个热门技术的交汇会碰撞出不一样的火花。 《Google Android SDK开发范例大全(第3版)》内容由Android的基础知识到实际开发应用,结构...

    Google Android SDK开发范例大全(第3版) 1/5

    特别是新增加了第11章来专门介绍HTML5技术在Android移动设备里的应用,相信当下两个热门技术的交汇会碰撞出不一样的火花。 《Google Android SDK开发范例大全(第3版)》内容由Android的基础知识到实际开发应用,结构...

    Google Android SDK开发范例大全(第3版) 4/5

    特别是新增加了第11章来专门介绍HTML5技术在Android移动设备里的应用,相信当下两个热门技术的交汇会碰撞出不一样的火花。 《Google Android SDK开发范例大全(第3版)》内容由Android的基础知识到实际开发应用,结构...

    Google Android SDK开发范例大全(第3版) 3/5

    特别是新增加了第11章来专门介绍HTML5技术在Android移动设备里的应用,相信当下两个热门技术的交汇会碰撞出不一样的火花。 《Google Android SDK开发范例大全(第3版)》内容由Android的基础知识到实际开发应用,结构...

    《Google Android SDK开发范例大全(第3版)》.pdf

    特别是新增加了第11章来专门介绍html5技术在android移动设备里的应用,相信当下两个热门技术的交汇会碰撞出不一样的火花。  《google android sdk开发范例大全(第3版)》内容由android的基础知识到实际开发应用,...

    Google Android SDK开发范例大全(第3版)part2

    特别是新增加了第11章来专门介绍HTML5技术在Android移动设备里的应用,相信当下两个热门技术的交汇会碰撞出不一样的火花。  《Google Android SDK开发范例大全(第3版)》内容由Android的基础知识到实际开发应用,...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

    Android开发之删除项目缓存的方法

    在项目中经常会使用到WebView 控件,当加载html 页面时,会在/data/data/应用package 目录下生成database与cache 两个文件夹。请求的url 记录是保存在WebViewCache.db,而url 的内容是保存在WebViewCache 文件夹下 打开...

    Google Android SDK开发范例大全的目录

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

    google android sdk开发范例大全 第二版 PDF 光盘代码

     5.16 取得目前File与Cache的路径   5.17 打开/关闭WiFi服务   5.18 取得SIM卡内的信息   5.19 调用拨号按钮   5.20 DPAD按键处理   5.21 任务 .管.理. 器正在运行的程序   5.22 动态更改...

    Google Android SDK 开发范例大全01

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

    Google Android SDK 开发范例大全02

    8.3 嵌入HTML标记的程序——WebView.loadData 8.4 设计前往打开网页功能——Intent与Uri.parse 8.5 将网络图像网址放入Gallery中显示——URL.URLConnection.BaseAdapter 8.6 即时访问网络图文件展示——...

Global site tag (gtag.js) - Google Analytics