After some testing, I realized that at the Android Webkit level, URLConnection is not used for the HTTP request, which means that the HttpResponseCache cannot automatically connect to the WebView like other native scripts.
So, I tried an alternative approach: use your own WebViewClient to connect WebView and ResponseCache:
webview.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(final WebView view, final String url) { if (! (url.startsWith("http://") || url.startsWith("https://")) || ResponseCache.getDefault() == null) return null; try { final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.connect(); final String content_type = connection.getContentType(); final String separator = "; charset="; final int pos = content_type.indexOf(separator);
If you need offline access to cached resources, just add the cache header:
connection.addRequestProperty("Cache-Control", "max-stale=" + stale_tolerance);
BTW, for this approach to work correctly, you need to properly configure the web server response using the "Cache-Control" header with the cache.
source share