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
将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);
}
} ```
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。