Android session management

android, apache-commons-httpclient, java, session, session-cookies

Solution

This has nothing to do with Android. It has everything to do with Apache HttpClient, the library you are using for HTTP access.

Session cookies are stored in your `DefaultHttpClient` object. Instead of creating a new `DefaultHttpClient` for every request, hold onto it and reuse it, and your session cookies will be maintained.

You can read about Apache HttpClient here and read about cookie management in HttpClient here.

Problem

Is there a specific library for Android session management? I need to manage my sessions in a normal Android app. not in `WebView`. I can set the session from my post method. But when I send another request that session is lost. Can someone help me with this matter? ``` DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("My url"); HttpResponse response = httpClient.execute(httppost); List<Cookie> cookies = httpClient.getCookieStore().getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } ``` When I try to access the same host that session is lost: ``` HttpGet httpGet = new HttpGet("my url 2"); HttpResponse response = httpClient.execute(httpGet); ``` I get the login page response body.

Original source