0

我想在执行 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。任何输入?

4

1 回答 1

1

尝试使用Singleton。问题是,当您创建 Class 时,只能存在该类的一个对象。你可以从任何地方获取这个对象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)...

}
于 2013-10-01T15:39:38.937 回答