Note that there are some explanatory texts on larger screens.

plurals
  1. POApache HttpClient: Cannot retrieve cookies sent from the server in Android device
    text
    copied!<p>I am having a strange problem while using Apache HttpClient in an Android app.</p> <p>My app needs to Login to a website and get some data and then logout.</p> <p>My current code looks like this:</p> <pre><code>public class BlueClient { private String hostUrl; private DefaultHttpClient client; private HttpContext localContext; public BlueClient(String hostUrl,DefaultHttpClient httpClient,HttpContext localContext) { this.hostUrl = hostUrl; this.client = httpClient; this.localContext = localContext; } public boolean doLogin(String userName,String password){ String url = getHostUrl()+"do_login";//loggin will set a session cookie HttpPost post = new HttpPost(url); try { List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2); nameValuePairs.add(new BasicNameValuePair("username",userName)); nameValuePairs.add(new BasicNameValuePair("password",password)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = client.execute(post,localContext); //ok now if response is ok then return true } catch (Exception e) { } return false; } public MyStuff getMyStuff(){ String url = getHostUrl()+"/getMyStuff/"; //this url requires authentication. the sesion cookie should do that HttpGet get = new HttpGet(url); try { HttpResponse response = client.execute(get,localContext); //ok get my stuff from response and return } catch (Exception e) { } return null; } public boolean doLogout(){ String url = getHostUrl()+"do_logout";//it clears the cookie so the session is invalidated HttpGet get = new HttpGet(url); try { HttpResponse response = client.execute(get,localContext); //ok the cookie is cleared } } catch (Exception e) { } return false; } } </code></pre> <p>And when i call these function i do like this. <strong>It works in emulator but not in device</strong></p> <pre><code>HttpContext context = new BasicHttpContext(); CookieStore cookieStore = new BasicCookieStore(); context.setAttribute(ClientContext.COOKIE_STORE,cookieStore); DefaultHttpClient httpClient = new DefaultHttpClient(); BlueClient myClient = new BlueClient("http://myHost.com/",httpClient,context); myClient.doLogin("user","pass"); // it should've printed the cookies set by the server but i get nothing here ! D.log(context.getAttribute(ClientContext.COOKIE_STORE)); // as this is another subsequesnt request it shoud carry the cookies back to the server but as the cookies are not set this function gives me nothig :-( myClient.getMyStuff(); myClient.doLogout(); </code></pre> <p>Can anyone please shed some light on this. Why its not working in the device?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload