当 html 内容发生变化时,如何让 webview 自动刷新其缓存?

top*_*bec 1 android caching webview

在我的 android 应用程序中,我设置

webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

为 webview 启用缓存。问题是,当网络内容发生变化时(我不知道),它仍然使用旧的缓存内容。如何在不使用缓存的情况下更改在线 Web 内容时自动重新缓存内容?

任何建议将不胜感激。

Shr*_*ant 5

我相信您无法确定网页内容是否已更改。

但是,当您启用缓存到 webview 时,它会自动为 webview 缓存提供一个过期时间,这意味着,如果 webView 缓存过期,它将再次加载网页。如果您使用,这是可能的WebSetting.LOAD_DEFAULT

如果您觉得 html 页面的内容可能会更频繁地更改,那么您不应该使用WebSetting.LOAD_CACHE_ELSE_NETWORK因为,根据 doc:

public static final int LOAD_CACHE_ELSE_NETWORK

Use cached resources when they are available, even if they have expired. Otherwise load resources from the network. 
Run Code Online (Sandbox Code Playgroud)

即使它已过期,这也会加载缓存版本的 html 页面。