如何启用过期头缓存的WebView [英] How to enable expires-header caching for webview

查看:229
本文介绍了如何启用过期头缓存的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立它由一个菜单和网页视图的应用程序。当用户选择菜单项,所述网页视图应该加载尊重HTML文件。到目前为止好。

现在我遇到,那Webview是每个请求我pressing菜单项时的HTML。我想只有在一个会话一次加载HTML,导致HTML文件在一天不会改变。所以,我做的第一件事就是设置在服务器端正确Expires头。您可以点击此处查看:

<一个href=\"http://redbot.org/?uri=http%3A%2F%2Fcutoutcam.com%2Ftest1.php\">http://redbot.org/?uri=http%3A%2F%2Fcutoutcam.com%2Ftest1.php

然后我试图

  mWebView.getSettings()setCacheMode(WebSettings.LOAD_DEFAULT)。

  mWebView.getSettings()setCacheMode(WebSettings.LOAD_CACHE_ONLY)。

结果:

第一个版本要求HTML每次(与代理选中) - >这是不可思议。只要它没有过期它应该显示缓存的版本。有什么问题?

第二个版本从未请求一个新的HTML文件(这就是好的,因为它是应该到)

任何人有一个想法,为什么expires头在此处不正常工作?

整个code:

  mWebView =(的WebView)getView()findViewById(R.id.fragment_web_view_wv)。
mWebView.setWebViewClient(新WebViewClient(本));
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);mWebView.setWebChromeClient(新WebChromeClient(){
      @覆盖
      公共无效onReachedMaxAppCacheSize(长spaceNeeded,长tot​​alUsedQuota,
              android.webkit.WebStorage.QuotaUpdater quotaUpdater)
      {
            quotaUpdater.updateQuota(spaceNeeded * 2);
      }
});mWebView.getSettings()setDomStorageEnabled(真)。
mWebView.getSettings()setAppCacheMaxSize(1024 * 1024 * 8)。
串appCachePath = getActivity()getApplicationContext()getCacheDir()getAbsolutePath()。;
mWebView.getSettings()setAppCachePath(appCachePath)。
。mWebView.getSettings()setAllowFileAccess(真);
mWebView.getSettings()setAppCacheEnabled(真)。
。mWebView.getSettings()setCacheMode(WebSettings.LOAD_CACHE_ONLY);
mWebView.getSettings()setDefaultZoom(WebSettings.ZoomDensity.FAR)。
mWebView.loadUrl(args.getString(URL));


解决方案

大多数现代浏览器总是会向服务器请求,即使缓存的内容,只是为了检查,如果服务器已经更新可用的内容。在这种情况下,浏览器将包括如果-Modified-Since的的请求头,使服务器能够快速,如果一切都没有改变返回回一个空的HTTP 304响应。

您选项是
1)配置您的服务器,以评估的Last-Modified-Since和返回304为宜。这将告诉浏览器继续使用缓存的内容。
2)使用JavaScript实现你的页面加载并创建localStorage的自定义缓存机制,不受浏览器厂商的率性。这是一些工作,但我已经在几个性能敏感的项目顺利完成。

I am building an app which consists of a menu and a webview. When the user is selecting menu items, the webview should load the respecting html file. So far so good.

Now I am experiencing, that the webview is requesting the html each time I am pressing the menu item. I would like to only load the html once in a session, cause the html files wont change during a day. So first thing I did is to set the expires header correctly on the server side. You can check it here:

http://redbot.org/?uri=http%3A%2F%2Fcutoutcam.com%2Ftest1.php

Then I tried

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

and

mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);

results:

first version requests the html each time (checked with a proxy) -> that's weird. it should show the cached version as long as it's not expired. what's the problem?

second version never requests a new html file (thats ok, cause it's supposed to to that)

Anyone has an idea why the expires header does not work here correctly?

The whole code:

mWebView = (WebView) getView().findViewById(R.id.fragment_web_view_wv);
mWebView.setWebViewClient(new WebViewClient(this));
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);



mWebView.setWebChromeClient(new WebChromeClient() {
      @Override
      public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,
              android.webkit.WebStorage.QuotaUpdater quotaUpdater)
      {
            quotaUpdater.updateQuota(spaceNeeded * 2);
      }
});

mWebView.getSettings().setDomStorageEnabled(true);


mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);


String appCachePath = getActivity().getApplicationContext().getCacheDir().getAbsolutePath();
mWebView.getSettings().setAppCachePath(appCachePath);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
mWebView.loadUrl(args.getString("url"));

解决方案

Most modern browsers will always make a request to the server, even for cached content, just to check if the server has updated content available. In these cases, the browser will include an "If-Modified-Since" header in the request so that the server can quickly return back an empty HTTP 304 response if nothing has changed.

Your options are either 1) Configure your server to evaluate Last-Modified-Since and return 304 as appropriate. This will tell the browser to go ahead and use the cached content. 2) Implement your page loading using javascript and create a custom caching mechanism with localstorage which isn't subject to the whims of browser vendors. This is a bit of work but what I've done successfully on several performance-sensitive projects.

这篇关于如何启用过期头缓存的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆