0
votes

I've been struggling trying to maintain a session after a post login and I was wondering if someone could help. I was going to try Apache, but the android documentation states they have moved away from Apache in favor of Java's URLConnection.

  1. If I have cookie data stored as a bundle extra, how can I take that data and add it back into the CookieManager?

  2. Is it bad form to store the cookie data into a string and then pass it to my next activity using the bundle extras?

  3. Do I have to create a CookieManager in every activity and basically store it after each request, reassign it, then issue my next request?

    //Login.java: private CookieManager loginCookie = new CookieManager(); extras.putString("Cookies", loginCookie.toString());

    //LoggedIn.java:
    Bundle bundle = intent.getExtras();
    String strCookies = bundle.getString("Cookies");
    SummaryCookies = (CookieManager)strCookies; //I tried casting the strCookies back to a CookieManager, but that did not work. :(
    

How can I get my cookies from the strCookies and convert it back into a format that the CookieManager understands?

1

1 Answers

1
votes

1+2) I don't think it is a good way to work with a Cookie, because when you stop the app, this cookie also will be deleted. You should save them to persistent storage and use them later.

3) You have to implement your own CookieStore, then create a CookieManager and set its default with CookieHandler.setDefault(...). This applies to the whole-system so you shouldn't create it for every activity.

4) If you still want to use your method. You could get a Cookie from the response header by the getHeaderFields() method, and query "Set-Cookie", and "Set-Cookie2" header. Build your header String like that "Set-Cookie:Your-string-cookie-here" and use HttpCookie.parse(headerString) to get your list HttpCookie object.

This is an example for create persistent cookie store. Here is the full source if you need: https://github.com/nguyentrungduy/Customize-CookieStore

@Override
public void add(URI uri, HttpCookie cookie) {
    // TODO Auto-generated method stub
    synchronized (this) {
        URI uri2 = createURI(uri);
        List<HttpCookie> cookies = mCookies.get(uri2);
        if (cookies == null) {
            cookies = new ArrayList<HttpCookie>();
            mCookies.put(uri2, cookies);
        } else {
            cookies.remove(cookie);
        }
        cookies.add(cookie);
        // Save into SharePreferenced
        saveToPersistent(uri2, cookies);
    }
}

@Override
public List<HttpCookie> get(URI uri) {
    // TODO Auto-generated method stub
    synchronized (this) {
        List<HttpCookie> httpCookies = new ArrayList<HttpCookie>();
        List<HttpCookie> values = mCookies.get(uri);
        if (values != null) {
            for (HttpCookie httpCookie : values) {
                if (!httpCookie.hasExpired()) {
                    httpCookies.add(httpCookie);
                }
            }
        }

        Set<Entry<URI, List<HttpCookie>>> set = mCookies.entrySet();
        for (Entry<URI, List<HttpCookie>> entry : set) {
            if (uri.equals(entry.getKey())) {
                continue;
            }
            List<HttpCookie> cookies = entry.getValue();
            Iterator<HttpCookie> iterator = cookies.iterator();
            while (iterator.hasNext()) {
                HttpCookie cookie = iterator.next();
                String domain = cookie.getDomain();
                if (!domainMatches(domain, uri.getHost())) {
                    continue;
                }
                if (cookie.hasExpired()) {
                    iterator.remove();
                } else if (!httpCookies.contains(cookie)) {
                    httpCookies.add(cookie);
                }
            }
        }
        return Collections.unmodifiableList(httpCookies);
    }
}