Java android - CookieHandler - How to keep cookies after closing application?

To save cookies after every request in the HttpURLConnection, you need to add a CookieHandler to your application starting with:

CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

      

But in the application, closing and opening again the cookie is empty ... So how to save the cookies after closing?

Something like storing them in SharedPreferences or in a file and returning them after opening ...

I tried to store them using CookieStore, but it didn't work: ...

Save

Settings.Save(c, TAG, cookieManager.getCookieStore().getCookies().toString());

      

Load:

String load = Settings.Load(c, TAG);
if (load != null) {
    for (HttpCookie hc : HttpCookie.parse(load)) {
        cookieManager.getCookieStore().add(new URI(Data.domain), hc);
    }
}

      

Thank..

+3


source to share


1 answer


CookieStore

Not saved to disk by default , you need to implement the one that does. Here's an example implementation that stores Cookies directly in SharedPreferences.



+6


source







All Articles