Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java android - CookieHandler - How to keep cookies after closing the app?

To keep cookies after each request in HttpURLConnection, should to add CookieHandler on the app starting:

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

But in the app closing and opening again the cookies are empty... So how to save the cookies after the closing?

Something like save them in the SharedPreferences or in the file and get them back after the opening...

I tryed to keep them using CookieStore, but that's not worked:...

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

Thanks..

like image 466
Eli Avatar asked Feb 09 '23 13:02

Eli


1 Answers

The default CookieStore does not persist anything to disk, you need to implement one which does. Here is an example implementation which saves Cookies directly to SharedPreferences.

like image 200
BladeCoder Avatar answered Apr 28 '23 00:04

BladeCoder