Android cache loading does not work. Show WebView Offline

I am trying to display some web pages through a webview without an internet connection. I thought I could save web pages in the cache and download them again from the cache if there is no Internet connection. But that does not work. The website does not appear, instead I understand that the website is not available right now. I already checked if AppCachePath using getCacheDir() . Do you have an idea what I'm doing wrong or how it works. It would be great. Thank you very much.

 webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setAppCacheMaxSize(1024*1024*8); webView.getSettings().setAppCachePath("/data/data/de.app/cache"); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setAppCacheEnabled(true); webView.setWebChromeClient(new WebChromeClient() { @Override public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { quotaUpdater.updateQuota(spaceNeeded * 2); } }); ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); if(cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected() == true) { webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); webView.loadUrl("http://www.google.de"); } else{ webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY); webView.loadUrl("http://www.google.de"); } 
+4
source share
1 answer

you can try using webSettings.setCacheMode (WebSettings.LOAD_CACHE_ELSE_NETWORK);

+3
source

Source: https://habr.com/ru/post/1379850/


All Articles