如何在android webview中禁用缓存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21059631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:55:29  来源:igfitidea点击:

How to disable cache in android webview?

androidwebview

提问by user3153083

In my android webview my webpage is loading even without internet because of cache, so i want to disable cache in android webview, can anyone help me how to do this?

在我的 android webview 中,由于缓存,我的网页即使在没有互联网的情况下也能加载,所以我想在 android webview 中禁用缓存,任何人都可以帮助我如何做到这一点?

I am using following lines in my webview, but still i am not able to disable cache.

我在我的 webview 中使用了以下几行,但我仍然无法禁用缓存。

     myBrowser.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
     myBrowser.getSettings().setAppCacheEnabled(false); 

Please suggest me if there are some other methods

请建议我是否还有其他方法

回答by Piyush

Please add below code

请添加以下代码

        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

Please remove app from history task and test its working.

请从历史任务中删除应用程序并测试其工作。

回答by Mellson

Just after creating your webview, before loading any pages, you can clear the cache.

在创建 webview 之后,在加载任何页面之前,您可以清除缓存。

myBrowser.clearCache(true)- the boolean indicates if you wish to delete the cached files on disk as well.

myBrowser.clearCache(true)- 布尔值表示您是否也希望删除磁盘上的缓存文件。

The documentation is hereif you wish to dig further.

如果您想进一步挖掘,文档就在这里

回答by Firewall_Sudhan

I had the same problem. Even though I had the below two lines, my webview was still loading old data.

我有同样的问题。即使我有以下两行,我的 webview 仍在加载旧数据。

webview.getSettings().setAppCacheEnabled(false); webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

webview.getSettings().setAppCacheEnabled(false); webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

My mistake was, i had this cache related settings after I called the loadUrl() on webview. After I moved these cache settings ahead of loadUrl(), i had everything working as expected.

我的错误是,在 webview 上调用 loadUrl() 后,我有了与缓存相关的设置。在我将这些缓存设置移动到 loadUrl() 之前,我一切都按预期工作了。