Persistent Cookie Store So my app needed a cookie store that persists over the app closing, device reboot etc. I decided to use the sharedPreferences for this. The class works pretty well, in both my tests and on the device. However there are couple of things I think can be improved. First is error handling. The problem is I cannot throw the errors further up in the app as the cookiestore interface doesn't not allow it. Secondly I have made a persistantcookie so I have a concrete class to serialise. I then convert that to the cookie interface. It seems an ugly way to do it. So any pointers would be very much appreciated.

public class BetterPersistantCookieStore implements CookieStore {

    private static final String TAG = BetterPersistantCookieStore.class.getSimpleName();
    private SharedPreferences sharedPreferences;
    private ObjectMapper objectMapper = new ObjectMapper();//TODO pass serializer and deserialiser into constructor
    private static final String COOKIES_KEY = "cookiesKey";

    @Inject
    public BetterPersistantCookieStore(SharedPreferences sharedPreferences) {
        this.sharedPreferences = sharedPreferences;
    }

    @Override
    public void addCookie(Cookie cookie) {
        List&lt;PersistantCookie&gt; cookieList = getPersistantCookies();
        int len = cookieList.size();
        for (int i = 0; i &lt; len; ++i) {
            Cookie tempCookie = cookieList.get(i);
            if (cookie.getName().equals(tempCookie.getName())) {
                cookieList.remove(i);
            }
        }
        cookieList.add(new PersistantCookie(cookie));
        sharedPreferences.edit().putString(COOKIES_KEY, listToString(cookieList)).commit();
    }

    @Override
    public List&lt;Cookie&gt; getCookies() {
            return convertPersistantCookiesToCookies(getPersistantCookies());
    }

    @Override
    public boolean clearExpired(Date date) {
        List&lt;PersistantCookie&gt; cookieList = getPersistantCookies();
        int len = cookieList.size();
        boolean returnValue = false;
        for (int i = 0; i &lt; len; ++i) {
            Cookie cookie = cookieList.get(i);
            if (cookie.isExpired(date)) {
                cookieList.remove(i);
                returnValue = true;
            }
        }
        sharedPreferences.edit().putString(COOKIES_KEY, listToString(cookieList)).commit();
        return returnValue;
    }

    @Override
    public void clear() {
        sharedPreferences.edit().remove(COOKIES_KEY).commit();
    }

    private List&lt;PersistantCookie&gt; getPersistantCookies()  {
            return stringToList(sharedPreferences.getString(COOKIES_KEY, ""));
    }

    private List&lt;PersistantCookie&gt; stringToList(String value) {
        try {
            return objectMapper.readValue(value, TypeFactory.collectionType(ArrayList.class, PersistantCookie.class));
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList&lt;PersistantCookie&gt;(0);
        }
    }

    private String listToString(List&lt;PersistantCookie&gt; cookies)  {
        try {
            return objectMapper.writeValueAsString(cookies);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    private List&lt;Cookie&gt; convertPersistantCookiesToCookies(List&lt;PersistantCookie&gt; persistantCookies) {
        List&lt;Cookie&gt; cookies = new ArrayList&lt;Cookie&gt;();
        for (Cookie cookie : persistantCookies) {
            cookies.add(cookie);
        }
        return cookies;
    }

}
First is error handling. The problem is I cannot throw the errors further up in the app as the cookiestore interface doesn't not allow it. 


You can define a custom unchecked exception and throw that.  (Or throw an existing unchecked exception if you can find one that is suitable.)


  Secondly I have made a persistantcookie so I have a concrete class to serialise. I then convert that to the cookie interface. It seems an ugly way to do it.


I don't think you have any sensible alternatives.  (And besides, I disagree that it is "ugly".)



And please correct the spelling of (at least) the public class and method names.  It is like chalk screeching on a blackboard ...