Android : Working with cookie manager and cookie store in android

on Sunday, February 15, 2015


First let me start by saying that I've read a lot about it in StackOverflow but I still cant get it right in my head so I'm asking for your help.


My app has a webview for the initial login phase, which sets a cookie. That cookie will be used for further calls to the service but using NATIVE calls (not through the webview).


So I've implemented my own CookieStore and I've set "java.net.CookieManager" with the instance of my custom cookie store and I've set the default handler. So far so good.


I can see that the webview sets the cookie using my implementation (I print logs at the cookieStore class).


So android automatically calls the add method on my cookieStore which looks like this:



@Override
public void add(URI uri, HttpCookie cookie) {
//TODO check if cookie already exists
cookieStore.add(uri, cookie);
}


Now after the page (in the webview) has finished loading,I'm adding the cookie and then I want to check if the cookie was set and then to use it for future service calls:



HttpCookie cookie = new HttpCookie(url, url);
cookieManager.getCookieStore().add(URI.create(url), cookie);


List<HttpCookie> cookies = cookieManager.getCookieStore().get(URI.create(url));
Log.d("test", "COOKIE SIZE " + cookies.size());
for (HttpCookie cookier : cookies) {
Log.d("Cookie", "DOMAIN - " + cookier.getDomain() + "NAME - " + cookier.getName());
}


I'm getting size 0 for the HttpCookie list which I know is wrong and there are cookies set.


This is what logcat prints:



URI ADD﹕ URI // I removed actual url from this text for security reason
URI GET﹕ URI
COOKIE SIZE 0


As you can see the URI is first added and then immediately I call get and they are the same. The result set returned from "get" is 0. What is wrong?


0 comments:

Post a Comment