1

I use httpclient along with cookiestore to keep my session, now I want to use the same session on the next activity, I'm using api 8 so I can't use cookiemanager. Is it possible? If only I could somehow send the cookie list through, eg:

Intent i = new Intent(this, Login.class);
i.putExtra("domain", domain);
//need to get the following list across
List<Cookie> cookies = cookieStore.getCookies();
//i.putMyDamnCookies("cookies",cookies);
startActivity(i);

Any idea how I could achieve this?

Stuyvenstein
  • 2,340
  • 1
  • 27
  • 33

3 Answers3

2

Yes you can send a List to another activity, but first you'll need to convert it to an instance of ArrayList, or String[] array.

Take a look over this threads:
Passing a List to another Activity in Android
How to put a List in intent

Community
  • 1
  • 1
Andy Res
  • 15,963
  • 5
  • 60
  • 96
1

store your List as an array of strings and pass it to in an intent to next activity like this:

String[] cookieArray = new String[cookies.size()];
            //copy your List of Strings into the Array 
            int i=0;
            for(Cookie c : cookies ){
                cookieArray[i]=c.toString();
                i++;
             }
            //then pass it in your intent
            Intent intent = new Intent(this, Login.class);
            intent.putExtra("cookieArray", cookieArray);
            startActivity(i);  

Then in your next actvity, retrieve the array of cookies from the intent and convert the cookies back like so:

List<Cookie> cookies = new List<Cookies>();
for(int i=0;i<cookieArray.size;i++)
{
cookies.add(new HttpCookie(cookieArray[i]));
}
Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Thanx but once I sent it to the new activity, how do I convert from strings back to cookies? And remember to put `i++;` in your for loop – Stuyvenstein Oct 07 '12 at 16:53
0

Sure - just read the cookie from the HTTP header, and store it however is convenient for you.

I think this is probably overkill, but here's an example that uses the Apache HTTP Client that ships with Android 2.2:

Also look here (available since level 1):

Community
  • 1
  • 1
paulsm4
  • 114,292
  • 17
  • 138
  • 190