简体   繁体   中英

Android WebView - clear cache

I have a WebView control and I want to show every time a current site, but I think the WebView show only the cached version of the site. I tried everything found here but nothing seems to work. I tried also to delete all files from the context.getCacheDir(), but without success. What can I do?

This is my code from onCreate method:

    clearCache(this, 0);

    setContentView(R.layout.webview);

    deleteDatabase("webview.db");
    deleteDatabase("webviewCache.db");

    mWebView = (WebView)findViewById(R.id.webview);
    mWebView.setWebViewClient(new CenterWebViewClient(this));
    mWebView.setWebChromeClient(new CenterWebChromeClient(this));
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(false);
    mWebView.getSettings().setAppCacheMaxSize(1);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    String  url = "https://www.exacmple.com";

    if (url != null) {
        Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
        noCacheHeaders.put("Pragma", "no-cache");
        noCacheHeaders.put("Cache-Control", "no-cache");
        mWebView.loadUrl(url, noCacheHeaders);
    }

clearCache function:

static int clearCacheFolder(final File dir, final int numDays) {

    int deletedFiles = 0;
    if (dir!= null && dir.isDirectory()) {
        try {
            for (File child:dir.listFiles()) {

                //first delete subdirectories recursively
                if (child.isDirectory()) {
                    deletedFiles += clearCacheFolder(child, numDays);
                }

                //then delete the files and subdirectories in this dir
                //only empty directories can be deleted, so subdirs have been done first
                if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                    if (child.delete()) {
                        deletedFiles++;
                    }
                }
            }
        }
        catch(Exception e) {
            Log.e("WebView", String.format("Failed to clean the cache, error %s", e.getMessage()));
        }
    }
    return deletedFiles;
}

    public static void clearCache(final Context context, final int numDays) {
         Log.i("WebView", String.format("Starting cache prune, deleting files older than %d days", numDays));
         int numDeletedFiles = clearCacheFolder(context.getCacheDir(), numDays);
         Log.i("WebView", String.format("Cache pruning completed, %d files deleted", numDeletedFiles));
    }

It seems like you can clear the cache whenever the webview is shown

 @Override
public void onPageFinished(WebView view, String url) {
     super.onPageFinished(view, url);
     view.clearCache(true);
}

via - https://stackoverflow.com/a/10701961/2250339

这不是缓存的问题,而是服务器端的会话问题,我不得不删除cookie,现在它可以工作了。

To clear the cookies form your APP:

CookieSyncManager.createInstance(this);         
CookieManager cookieManager = CookieManager.getInstance();        
cookieManager.removeAllCookie();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM