1

I searched a lot now for the persistence of Cookies but cant find a good solution.

I use HTTPUrlConnection to authenticate against a server and I get Cookies back. I retrieve them in CookieManager and can load them into a new connection. Now I want to persistent that Cookies, perhaps the whole Cookiemanager Object. I found a solution, that you can persist the Cookies with specifying a CookieStore by creating the CookieManager.

I found only old solutions (2-3 years) that say you have to build your own persistent CookieStore, since a persistent CookieStore is not implemented in the SDK? Is this up to date? Is there already a persistent CookieStore implemented in the SDK or does I have to persist the Cookies by myself with SharedPreferences? Or does anybody has a better solution to persist Cookies nowadays?

Best Regards,

Rei
  • 11
  • 3

1 Answers1

0

This seems to be answered in some other palces, I didn't try it but you can get a detailed explain in this link:

http://blog.winfieldpeterson.com/2013/01/17/cookies-in-hybrid-android-apps/

public class YourApplication extends Application {
  public void onCreate() {
    super.onCreate();

    //Setup Cookie Manager and Persistence to disk
    CookieSyncManager.createInstance(this);
    CookieManager.getInstance().setAcceptCookie(true);
  }
}

public class MainActivity extends BaseActivity {
  public void onResume() {
    CookieSyncManager.getInstance().stopSync();
  }

   public void onPause() {
     CookieSyncManager.getInstance().sync();
   }
}

API21 deprecates CookieSyncManager, now to ensure that cookies are written to disk use:

CookieManager.flush()
  • Thanks, but CookieSyncManager was deprecated in API21. – Rei Sep 13 '15 at 16:25
  • I add a API21 wokarround to the article, I not tested it. I hope this helps. – Marcos Lois Bermúdez Sep 15 '15 at 15:29
  • Thank you very much. It seems that you can write to persistant storage with CookieManager.flush() but CookieManager doesnt has a method to retrieve from persistant storeage? I think perhaps you have to write a Cookie Storage by yourself and CookieManager.flush() will write it to the CookieStore when you defined a CookieStore in the initialisation of CookieManager and calls up a method of your custom CookieStore. I will proof that later. Best regards. – Rei Sep 15 '15 at 21:24