I have an activity that lets you sign in to a page. In the next activity it should display a webpage based on the cookie if the login was successful. The cookie is retrived
First of all, make sure to read the header values that your website gives you. You can determine this via Wireshark.
Secondly, when you know how the cookie look like (based from your Wireshark values), you should try to achieve the same values in your code. Make sure to debug the cookie's value(s) and make adjustments on the cookie itself if it doesn't match.
You have used this line -
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
}
. To ensure you receive new cookie everytime.
Seems like you have gone through same issue as I faced, check below link -
removeSessionCookie() issue of android (code.google.com)
it says that removeSessionCookie()
is implemented in a thread, so whenever it is called; a thread starts and after your setCookie(url, cookieString);
is called, it removes the new cookie you just set.
So for some devices it works well as removeSessionCookie()
is already executed, while, for some, it remove the cookie, and we get that problem.
by using SystemClock.sleep(500);
, you just gave system to finish removeSessionCookie() first
I suggest you remove this removeSessionCookie();
as you are setting only one cookie, so it won't conflict with other cookies. Your code will work seamlessly.