3

What is the most simple way to implement persistent cookie store on retrofit? Now I am using this:

    cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

but I need cookies to be saved and restored any other time.

Jonas
  • 3,329
  • 1
  • 33
  • 56

1 Answers1

3

You would need to build your own CookieStore implementation. The implementation would be up to you. You could choose to persistent the data into SharedPreference, flat file, JSON, XML ...

It would look something like so.

public class MyCookieStore implements CookieStore {
    // Implementation goes here
}

Then you can simply use it like so

CookieManager cookieManager = new CookieManager(new MyCookieStore(), CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);

Here's an SO answer which attempted to created its own SharedPreference implementation. It might help you get started.

Community
  • 1
  • 1
Miguel Lavigne
  • 18,143
  • 8
  • 52
  • 46