The βrightβ way to do this is to implement CookieHandler: http://developer.android.com/reference/java/net/CookieHandler.html
The easiest way to do this is to extend the application and put it in your onCreate () applications:
CookieHandler.setDefault(new CookieManager());
PLEASE NOTE:. Only DEFAULT CookieManger will be implemented. By default, CookieManger will manage cookies for all of your HTTP requests for a specific session of your application. However, it does not have any means of storing cookies the next time you use the application.
To do this, you need to write your own cookie manager by implementing CookieStore: http://developer.android.com/reference/java/net/CookieStore.html
Here is an example implementation of CookieStore that I used in an application that is currently on the Google Play store:
package com.touchvision.util; import java.net.CookieStore; import java.net.HttpCookie; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import com.touchvision.Config; public class TvCookieStore implements CookieStore { private static final String LOGTAG = "TV-TvCookieStore"; private Map<String, Map<String,String>> mapCookies = new HashMap<String, Map<String,String>>(); private final SharedPreferences sharedPrefs; public void add(URI uri, HttpCookie cookie) { String domain = cookie.getDomain();
The above custom CookieStore uses SharedPreferences to save cookies. You implement the above class, similar to how you would use the default CookieManager in your application class, but the line will look like this:
CookieHandler.setDefault( new CookieManager( new TvCookieStore(this), CookiePolicy.ACCEPT_ALL));
As you can see, the only cookie that really cared about saving was the Spring Cookie security (we used the Spring Framework on the server side). Obviously, your code will be different from your specific needs.
One more note: I tried many times to do what you are doing and handle the storage of cookies in my http client class. It was just a headache. Give this strategy a shot.