问题描述
|
我正在开发一个Android应用程序,该应用程序除其他功能外还可以在Webview中显示网站。据我了解,如果无法建立连接,则网络视图会自动显示页面的缓存版本。
有什么方法可以确定显示的页面是否已从服务器或缓存中获取?
甚至缓存页面的年龄。
这样可以通知用户他/她正在查看旧信息。
解决方法
您可以尝试破解-首先将WebView的缓存模式设置为“ 0”并加载URL。然后,创建一个自定义WebChromeClient,如下所示:
final String urlToLoad = \"http://www.my-url.com\";
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view,int errorCode,String description,String failingUrl)
{
if (view.getSettings().getCacheMode() == WebSettings.LOAD_NO_CACHE && urlToLoad.equals(failingUrl))
{
view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
view.loadUrl(urlToLoad);
return;
}
else
if (urlToLoad.equals(failingUrl))
{
// cache failed as well,load a local resource as last resort
// or inform the user
}
super.onReceivedError(view,errorCode,description,failingUrl);
}
});
webview.loadUrl(urlToLoad);
,由于WebView像浏览器一样工作,因此答案是否定的。
有一些方法可以检测到这种情况。
否则,另一种解决方案将是阻止页面被缓存(请参阅WebView文档)