androidcookie保存
Ⅰ android 怎麼清理 XWalkView 緩存的LocalStorage
WebView的緩存可以分為頁面緩存和數據緩存:
1,頁面緩存: >指載入一個網頁時的html、JS、CSS等頁面或者資源數據。 >這些緩存資源是由於瀏覽器的行為而產生,開發者只能通過配置HTTP響應頭影響瀏覽器的行為才能間接地影響到這些緩存數據。 >緩存的索引存放在/data/data/package_name/databases下。 >文件存放在/data/data/package_name/cache/xxxwebviewcachexxx下。
2,數據緩存 : >數據緩存分為AppCache和DOM Storage兩種。 >這些緩存資源是由開發者的直接行為而產生,所有的緩存數據都由開發者直接完全地掌控。 >Android中Webkit使用一個db文件來保存AppCache數據(my_path/ApplicationCache.db) >Android中Webkit會為DOM Storage產生兩個文件(my_path/localstorage/http_h5.m.taobao.com_0.localstorage和my_path/localstorage/Databases.db)。
那既然了解了,怎麼清除呢:
以下是網路出來的答案:
1.webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 2.context.deleteDatabase(「WebView.db」); 3.context.deleteDatabase(「WebViewCache.db」);4.webView.clearCache(true); 6.webView.clearFormData(); 7.getCacheDir().delete(); 8.用File的delete方法刪除緩存文件夾;12345
老實說,對我的問題,沒多大用,還是那句話,有事請Google
其實如果你只是想要每次用webView.loadUrl(url)載入新的頁面顯示,那麼調用webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE)就可以實現了,並不需要去刪除緩存文件什麼的。
但是我的項目中使用了JavaScript的交互,而JavaScript的載入是通過頭文件去獲取載入的,所以每次我去獲取新的頁面的時候,雖然頁面是新的,但是頭文件中的數據還是old的,所以每次載入到的js都是old的。那麼如何去做呢?
上面提到這個頭文件是瀏覽器HTTP相應頭去獲取的,開發者只能間接的影響,並不能控制。
所以單獨的webView.clearCache(true)是不能成功的,還需要清除webView的Cookie才行。
所以我最終的解決方法是在Activity的onDestroy()方法中添加如下代碼:
@Overrideprotected void onDestroy() { super.onDestroy(); //清空所有Cookie
CookieSyncManager.createInstance(QzmobileApp.getContext()); //Create a singleton CookieSyncManager within a context
CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
cookieManager.removeAllCookie();// Removes all cookies.
CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
webView.setWebChromeClient(null);
webView.setWebViewClient(null);
webView.getSettings().setJavaScriptEnabled(false);
webView.clearCache(true);
}
Ⅱ Android開發中怎樣使用cookieManager來管理cookie
前幾天剛研究過這個東西 我是用webview載入的頁面 我的做法就是 發送post請求登陸的時候 伺服器端如果登陸成功設置session 然後客戶端設置下cookies(這個主要告中伏是用那個JSESSIONID,因為客戶端和伺服器端識別是同一會話的方法就是這個JSESSIONID),設置cookies的方法 就參照6樓的寫法,然後用webview如下代碼設置主界面的cookies: Cookie sessionCookie = Httprequset.appCookie; //這里的cookie就是上面保存的cookie CookieSyncManager.createInstance(getApplicationContext()); CookieManager cookieManager = CookieManager.getInstance(); String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "培明 ;domain=" +sessionCookie.getDomain(); cookieManager.setCookie(url, cookieString); CookieSyncManager.getInstance().sync(); //載入地址 mWebView.loadUrl(url);這樣如果登陸成功的話你就可以在載入地址上直接獲取你設置session了 否則的話session取不到不知襪攜道能否幫到您! 查看原帖>>
Ⅲ android 瀏覽器cookie保存到哪裡
通過shareperfence保存,在瀏覽器的安裝文件下下面
Ⅳ android中 如何清理webview緩存
一、清除cookie
public static void clearCookies(Context context) {
// Edge case: an illegal state exception is thrown if an instance of
// CookieSyncManager has not be created. CookieSyncManager is normally
// created by a WebKit view, but this might happen if you start the
// app, restore saved state, and click logout before running a UI
// dialog in a WebView -- in which case the app crashes
@SuppressWarnings("unused")
CookieSyncManager cookieSyncMngr =
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}
這是facebook sdk的源碼,我不知道第一句到底起了什麼作用?
二、清除webview緩存,查看root過的手機data下的文件,會發現有這個東西:webview命名的東西
刪除保存於手機上的緩存.
// clear the cache before time numDays
private int clearCacheFolder(File dir, long numDays) {
int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
try {
for (File child:dir.listFiles()) {
if (child.isDirectory()) {
deletedFiles += clearCacheFolder(child, numDays);
}
if (child.lastModified() < numDays) {
if (child.delete()) {
deletedFiles++;
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
return deletedFiles;
}
打開關閉使用緩存
//優先使用緩存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//不使用緩存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
在退出應用的時候加上如下代碼
File file = CacheManager.getCacheFileBaseDir();
if (file != null && file.exists() && file.isDirectory()) {
for (File item : file.listFiles()) {
item.delete();
}
file.delete();
}
context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");
發現這個問題,一個朋友在iteye上問的:
Android的CookieManager只提供了removeAllCookies方法,用來刪除所有的cookie,有什麼辦法只刪除和特定url關聯的cookie呢?本來打算使用setCookie(url, value)將指定url關聯的cookie設為空串,但試了一下發現這個方法只是在已有的基礎上繼續添加cookie,並不能重置已有的cookie。
有朋友給打答案:
/**
* 同步一下cookie
*/
public static void synCookies(Context context, String url) {
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();//移除
cookieManager.setCookie(url, cookies);//指定要修改的cookies
CookieSyncManager.getInstance().sync();
}
Ⅳ android 嵌套webview請求介面的時候不在同一個session裡面怎麼處理
在開發一個項目時,遇到了這么一個問題,登錄界面是用Android的native的UI來做的,登錄成功後會跳轉到WebView來載入Sencha Touch的數據,但是在WebView到後台Load數據的時候發現,這時的session和登錄時的session不同了,因此取不到數據,為此google了很久,終於在StackOverflow找到了方法,在此先貼出原文地址:
其中關鍵點在下面的這些code里:
private DefaultHttpClient httpClient;
public static Cookie cookie = null;
//After Login
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (!cookies.isEmpty()) {
for (int i = 0; i < cookies.size(); i++) {
cookie = cookies.get(i);
}
}
//註:下面的code寫在WebView的Activity里
//------- Web Browser activity
Cookie sessionCookie = myapp.cookie; //這里的cookie就是上面保存的cookie
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName() + "=" + sessionCookie.getValue() + "; domain=" + sessionCookie.getDomain();
cookieManager.setCookie(myapp.domain, cookieString);
CookieSyncManager.getInstance().sync();
不多說了,主要是在登錄成功後,把cookie存起來,然後在WebView中SetCookie。
posted @ 2012-02-16 10:18 與時俱進 閱讀(...) 評論(...) 編輯 收藏
Ⅵ android webview 如何存儲cookie
在App中嵌入網頁,使用Nativie方式登錄,然後將cookie保存到WebView中,實現免登錄功能。同步Cookie到WebView的方法可以參考下面的代碼:
/**
* Sync Cookie
*/
private void syncCookie(Context context, String url){
try{
Log.d("Nat: webView.syncCookie.url", url);
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();// 移除
cookieManager.removeAllCookie();
String oldCookie = cookieManager.getCookie(url);
if(oldCookie != null){
Log.d("Nat: webView.syncCookieOutter.oldCookie", oldCookie);
}
StringBuilder sbCookie = new StringBuilder();
sbCookie.append(String.format("JSESSIONID=%s","INPUT YOUR JSESSIONID STRING"));
sbCookie.append(String.format(";domain=%s", "INPUT YOUR DOMAIN STRING"));
sbCookie.append(String.format(";path=%s","INPUT YOUR PATH STRING"));
String cookieValue = sbCookie.toString();
cookieManager.setCookie(url, cookieValue);
CookieSyncManager.getInstance().sync();
String newCookie = cookieManager.getCookie(url);
if(newCookie != null){
Log.d("Nat: webView.syncCookie.newCookie", newCookie);
}
}catch(Exception e){
Log.e("Nat: webView.syncCookie failed", e.toString());
}
}
使用上面的方法可以將Cookie同步到WebView中,這樣瀏覽網頁時即可實現免登錄。
但是在實際使用過程中會出現Cookie並未保存成功,每次都會跳轉到登錄頁面應為初始化WebView時漏掉了重要的東西。可以參考下面代碼設置WebView。
/**
* init WebView Settings
* */
private void initWebViewSettings(){
// myWebView.getSettings().setSupportZoom(true);
// myWebView.getSettings().setBuiltInZoomControls(true);
// myWebView.getSettings().setDefaultFontSize(12);
// myWebView.getSettings().setLoadWithOverviewMode(true);
// 設置可以訪問文件
myWebView.getSettings().setAllowFileAccess(true);
//如果訪問的頁面中有Javascript,則webview必須設置支持Javascript
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setUserAgentString(MyApplication.getUserAgent());
myWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setAppCacheEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
}
完成以上兩步操作,再次運行程序,就會發現,打開網頁後不會再跳轉到登錄頁面了。