我的一个活动中有一个WebView,当它加载一个网页时,该页面会从Facebook收集一些背景数据.
但我看到的是,每次打开并刷新应用程序时,应用程序中显示的页面都是相同的.
我已经try 将WebView设置为不使用缓存,并清除WebView的缓存和历史记录.
我也遵循了这个建议:How to empty cache for WebView?
但是这些都不管用,有没有人知道我可以克服这个问题,因为它是我应用程序的重要部分.
mWebView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress)
{
if(progress >= 100)
{
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
}
else
{
mProgressBar.setVisibility(ProgressBar.VISIBLE);
}
}
});
mWebView.setWebViewClient(new SignInFBWebViewClient(mUIHandler));
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.clearHistory();
mWebView.clearFormData();
mWebView.clearCache(true);
WebSettings webSettings = mWebView.getSettings();
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
Time time = new Time();
time.setToNow();
mWebView.loadUrl(mSocialProxy.getSignInURL()+"?time="+time.format("%Y%m%d%H%M%S"));
所以我实现了第一个建议(虽然将代码改为递归)
private void clearApplicationCache() {
File dir = getCacheDir();
if (dir != null && dir.isDirectory()) {
try {
ArrayList<File> stack = new ArrayList<File>();
// Initialise the list
File[] children = dir.listFiles();
for (File child : children) {
stack.add(child);
}
while (stack.size() > 0) {
Log.v(TAG, LOG_START + "Clearing the stack - " + stack.size());
File f = stack.get(stack.size() - 1);
if (f.isDirectory() == true) {
boolean empty = f.delete();
if (empty == false) {
File[] files = f.listFiles();
if (files.length != 0) {
for (File tmp : files) {
stack.add(tmp);
}
}
} else {
stack.remove(stack.size() - 1);
}
} else {
f.delete();
stack.remove(stack.size() - 1);
}
}
} catch (Exception e) {
Log.e(TAG, LOG_START + "Failed to clean the cache");
}
}
}
然而,这仍然没有改变页面显示的内容.在我的桌面浏览器上,我在WebView中生成的网页上得到了不同的html代码,所以我知道WebView一定在某个地方缓存.
在IRC频道上,我被指向一个修复程序,可以从URL连接中移除缓存,但我还不知道如何将其应用于WebView.
http://www.androidsnippets.org/snippets/45/
如果我删除我的应用程序并重新安装,我可以将网页恢复为最新版本,即非缓存版本.主要的问题是对网页中的链接进行了更改,所以网页的前端完全没有变化.