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();

7 comments:

Unknown said...

Hi, I find reading this article a joy. It is extremely helpful and interesting and very much looking forward to reading more of your work.. ACMarket

CNG said...

I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Action Link Wireless

Masood said...

Great post I would like to thank you for the efforts you have made in writing this interesting and knowledgeable article. Emus4u

DARYA KHAN said...

Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Cydia Impactor

CAPACITY said...

Great article with excellent idea!Thank you for such a valuable article. I really appreciate for this great information.. iFile IPA

2009 DREAM said...

Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! Download iOS 11.3

Anonymous said...

Since 1993, the on line casino has boosted the state and town’s income, while attracting gamblers from the Illinois and Kentucky sides of the river. When the machines were first legalized, local Christian activists clashed with gaming corporations, who had been lobbying the town to issue them gaming permits even prior to legalization. Jason Lee, the supervisor and co-owner of Chances, opened his gaming parlor in December 2019 along with his spouse after seeing the kind of money other places were bringing in. Lee and other enterprise homeowners said they don't appear to be|they aren't} responsible for gambling addictions. He compared his parlor to a bar — and bars aren’t responsible for controlling alcoholism, he said. They cluster along busy roads, tons of|and 원 엑스 벳 a lot of} gaming rooms are within strolling distance of one another.