android本地保存Cookie和还原到cookiestore

保存cookie,读取cookie以及判断cookie是否存在

```java

public class Local {

private static SharedPreferences shared;

public static void setShared(SharedPreferences shared) { Local.shared = shared; }

public static void saveStrCookie(String cookie) { SharedPreferences.Editor editor = shared.edit(); editor.putString(Constant.COOKIE_NAME, cookie); editor.commit(); }

public static boolean hasCookie() { String sessionid = shared.getString(Constant.COOKIE_NAME, null);

if (sessionid == null || sessionid.equals("")) { LogUtil.i("hasCookie return false"); return false; } LogUtil.i("hasCookie return true"); return true; }

public static String getCookie() { String sessionid = shared.getString(Constant.COOKIENAME, null); LogUtil.e("====>getCookie =" + _sessionid); return sessionid; }

public static void saveCookie(DefaultHttpClient httpClient) { List cookies = httpClient.getCookieStore().getCookies(); String strCookie = ""; for (Cookie cookie : cookies) { strCookie = cookie.getValue().toString(); } LogUtil.e("====>saveCookie = " + strCookie); saveStrCookie(strCookie); } } ```

将cookie加入CookieStore。 主要就是BasicClientCookie这个类

```java

private void setRequestCookies(DefaultHttpClient client) {

String cookie = Local.getCookie();

Log.i("COOKIE VALUE", cookie);

if (!TextUtils.isEmpty(cookie)) {

BasicClientCookie c = new BasicClientCookie(Constant.COOKIE_NAME,

cookie);

c.setVersion(0);

c.setPath("/");

c.setDomain(Constant.COOKIE_DOMAIN);

client.getCookieStore().addCookie(c);

}

} ```

android本地保存Cookie和还原到cookiestore,布布扣,bubuko.com

android本地保存Cookie和还原到cookiestore

上一篇:三:Java之Applet


下一篇:Trapping Rain Water