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"));
This example demonstrate about How to enable app cache for webview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
To use cache-control in HTML, you use the meta tag, e.g. The value in the content field is defined as one of the four values below. HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.
CacheWebView is a custom implement of Android WebView resource interceptor. It beyond system WebView cache space limit, let cache config more simple ,fast and flexible.
If you want to enable Cache-Control for all files, add Header set line outside the filesMatch block. As you can see, we set the Cache-Control header's max-age to 3600 seconds and to public for the listed files.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With