I want to use cookies stored in a list in a different activity which does a HTTPGET request.(make it global or persist )

My code which stores the cookies is:

  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);    

}

I want to use these cookies in a different activity which does a HTTP GET operation. Any input ?

有帮助吗?

解决方案

Try using Singleton. The thing is that when you create Class and only one object of this class can exist. And you can get this object from anywhere just calling CookieStore.getInstance() (thats how i called this class)

It would be smth like this :

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)...

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top