Caching in Webview is not working in Android

1.6k views Asked by At

When I open webview, it runs great when there is a network connection, but I have enabled caching in it, and without a network connection, it shows an error and loaded cached web page is not showing.

I have enabled caching and also set cache mode, but it's not working.

My webview code is below:

public class SettingsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.settings, container, false);
        WebView heroespage = (WebView) rootView.findViewById(R.id.webView1);
        WebSettings webSettings = heroespage.getSettings();
        webSettings.setAppCacheEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setJavaScriptEnabled(false);
        heroespage.setWebViewClient(new WebViewClientSubClass());
        heroespage.loadUrl("https://docs.google.com/spreadsheets/d/1E0FyHR_lCQce0NPnQL8z1XJp78XW6eLdO8CkeibTdgg/gviz/tq?tqx=out:html&tq&gid=2");
        return rootView;
    }
}
1

There are 1 answers

2
Jan On BEST ANSWER

You are not able to cache documents from docs.google.com like this.

The server can send a cache-control header as response to any request to set rules for the client about caching. As these documents from docs.google.com are never static, the response contains the instruction to don't cache your document.

cache-control:no-cache, no-store, max-age=0, must-revalidate

The Android WebView accept these rules and don't cache your site.