2013-04-16 200 views
1

我有一個WebView控件,我想每次顯示當前網站時,但我認爲WebView只顯示網站的緩存版本。我嘗試了一切發現here但似乎沒有工作。我也嘗試從context.getCacheDir()中刪除所有文件,但沒有成功。我能做什麼?Android WebView - 清除緩存

這是onCreate方法我的代碼:

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功能:

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)); 
    } 
+0

http://stackoverflow.com/questions/2465432/android-webview-completely-clear-the-cache。 http://stackoverflow.com/questions/6542943/clearing-all-cached-items-created-by-a-webview – Raghunandan

+0

我做到了,不起作用 –

+0

顯示你做了什麼 – Raghunandan

回答

1

這是一個緩存問題,只有在服務器端的會話,我不得不刪除cookies,現在它的工作。

+0

你是如何清除cookie的。您能否請您解釋 – Adi

+0

Hi @Adi,我現在不記得了,但我可能從瀏覽器設置中刪除了Cookie。 –

7

好像你可以清除緩存每當web視圖通過顯示

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

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

+1

該解決方案也提供了鏈接我給和不爲我工作。 –

0

要清除餅乾形成你的APP:

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