Friday, November 26, 2010

CookieManager and removeAllCookie problem

There was a requirement to send a special cookie to a web app through a WebView component in one of the applications I'm building recently.

I could set a cookie by using Android's CookieManager with the code below. This code worked well on the emulator (Donut, Eclair and Froyo) and on my devices HTC Magic with original Donut ROM and custom CyanogenMOD 6.0.0 ROM.

CookieSyncManager.createInstance(this);
CookieManager cm = CookieManager.getInstance();
cm.removeAllCookie();
cm.setAcceptCookie(true);
cm.setCookie(myCookieURI, "cookie_name=value");
CookieSyncManager.getInstance().sync();

The problem was when I tested this on some devices: Galaxy S and HTC Desire both running Froyo. The cookie was not being sent on those devices. After testing some modifications on the code I've found removeAllCookie() was removing the newly set cookie too. (Looks like there is a synchronization problem with CookieManager here?)

Removing removeAllCookie() from the code made everything work okay again. Hopefully, removing all cookies was not a requirement in this application. ;)

CookieSyncManager.createInstance(this);
CookieManager cm = CookieManager.getInstance();
cm.setAcceptCookie(true);
cm.setCookie(myCookieURI, "cookie_name=value");
CookieSyncManager.getInstance().sync();

1 comment:

chansara said...

I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. UC News Download