我想在执行HTTPGET请求的不同活动中使用存储在列表中的cookie。使其全局化或持久化)
我存储 cookie 的代码是:
HttpResponse sessionresp = httpclient.execute(httppost);
List<Cookie> cookielist=httpclient.getCookieStore().getCookies();
for (int i = 0; i < cookielist.size(); i++) {
Cookie cookie1 = cookielist.get(i);
Log.i("Cookie Store","Local cookie: " + cookie1);
//Cookie mSessionCookie = cookie1;
Log.i("Cookie Store",""+cookie1.getValue());
cookieStore.addCookie(cookie1);
httpclient2.setCookieStore(cookieStore);
}
我想在执行HTTP GET操作的不同活动中使用这些cookie。任何输入 ?
尝试使用 Singleton。问题是,当您创建类并且只能存在此类的一个对象时。你可以从任何地方获取这个对象,只需调用CookieStore.getInstance()
(这就是我称之为类的方式)
它会像这样:
class CookieStore {
private CookieStore(){}
private static CookieStore instance;
private List<Cookie> cookies;
public static CookieStore getInstance() {
if (instance == null) {
instance = new CookieStore();
}
return instance;
}
public List<Cookie> getCookies()...
public void setCookies(final List<Cookie> cookies)...
}