Android 4.4 Kitkat Not Receiving Cookies
In my application I send a POST request to a server, and receive a response from the server. And from the response I gather different cookies, user information specifically. So, I
Solution 1:
This code worked fine for me prior to KitKat 4.4 update -
HttpURLConnectionurlConnection= (HttpURLConnection) url.openConnection();
//handle cookiesCookieManagercookieManager=newCookieManager();
CookieHandler.setDefault(cookieManager);
It broke after 4.4.2 (at least that's when I noticed it), and cookies were no longer received. Simply moving the CookieManager and CookieHandler before opening the urlConnection fixed it again .. bizarre that it worked before! eg.
//handle cookiesCookieManagercookieManager=newCookieManager();
CookieHandler.setDefault(cookieManager);
HttpURLConnectionurlConnection= (HttpURLConnection) url.openConnection();
Solution 2:
I fixed the problem. I switched from HttpUrlConnection and other java.net stuff to AndroidHttpClient and other Apache Http classes. Cookies are now retrieved from Android API 19.
[EDIT] I used AndroidHttpClient (http://developer.android.com/reference/android/net/http/AndroidHttpClient.html) and I followed a couple Apache Http tutorials. So after I changed the code around a little bit, it looks like this:
private AndroidHttpClient httpClient; //Android Client, Uses User-Agent, and executes requestprivate HttpContext httpContext; //Contains CookieStore that is sent along with requestprivate CookieStore cookieStore; //Holds cookies from serverprivate HttpPost httpPost; //Contains message to be sent to clientprivatefinalStringUSER_AGENT= System.getProperty("http.agent"); //Unique User-Agent of current device//First call whenever connecting across the user's networkprivatevoidestablishConnection() {
cookieStore = newBasicCookieStore();
httpContext = newBasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); //Attach CookieStore to the HttpContext
getCookies(); //Retrieve currently stored cookies
httpClient = AndroidHttpClient.newInstance(USER_AGENT);
httpPost = newHttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("Cookie", sessionSeedCookie + ";" + sessionUidCookie + ";" + userLoginCookie + ";" + sPrefCookie);
}
//Called after communication is completeprivatevoiddisconnectAll() {
httpClient.close();
}
//Sends out POST request and returns an InputStreamprivate InputStream processRequest(String query)throws IOException{
httpPost.setEntity(newStringEntity(query)); //wraps the query into a String entityHttpResponseresponse= httpClient.execute(httpPost, httpContext); //Executes the request along with the cookie store
Log.i(TAG, "Server Response Code: " + response.getStatusLine().getStatusCode()); //Reads response code from server
updateCookies();
return response.getEntity().getContent();
}
//Logins User into Walkntradepublic String login(String email, String password)throws IOException {
establishConnection(); //Instantiate all streams and opens the connection
String query= "intent=login&password="+password+"&email="+email+"&rememberMe=true";
InputStreaminputStream= processRequest(query);
StringserverResponse= readInputAsString(inputStream); //Reads message response from server
disconnectAll();
return serverResponse;
}
Post a Comment for "Android 4.4 Kitkat Not Receiving Cookies"