How to enable expires-header caching for webview

2019-04-22 19:03发布

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"));

2条回答
贼婆χ
2楼-- · 2019-04-22 19:24

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-04-22 19:43

Use WebSettings.LOAD_CACHE_ELSE_NETWORK I think this is what you needed.

查看更多
登录 后发表回答