【问题标题】:Send cookies to next activity android将cookies发送到下一个活动android
【发布时间】:2012-09-28 01:07:38
【问题描述】:

我使用 httpclient 和 cookiestore 来保持我的会话,现在我想在下一个活动中使用相同的会话,我使用的是 api 8,所以我不能使用 cookiemanager。可能吗?如果我能以某种方式发送 cookie 列表就好了,例如:

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);

知道如何实现这一目标吗?

【问题讨论】:

    标签: android cookies android-activity


    【解决方案1】:

    当然 - 只需从 HTTP 标头中读取 cookie,然后按您方便的方式存储它。

    我认为这可能有点矫枉过正,但这里有一个使用 Android 2.2 附带的 Apache HTTP 客户端的示例:

    也看这里(从第 1 级开始可用):

    【讨论】:

      【解决方案2】:

      是的,您可以将List 发送到另一个活动,但首先您需要将其转换为ArrayListString[] 数组的实例。

      看看这个帖子:
      Passing a List to another Activity in Android
      How to put a List in intent

      【讨论】:

      • 您有任何将List&lt;Cookie&gt; 转换为List&lt;String&gt; 的指针吗?
      【解决方案3】:

      将您的列表存储为字符串数组,并将其传递给下一个活动,如下所示:

      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);  
      

      然后在你的下一个活动中,从意图中检索 cookie 数组并将 cookie 转换回来,如下所示:

      List<Cookie> cookies = new List<Cookies>();
      for(int i=0;i<cookieArray.size;i++)
      {
      cookies.add(new HttpCookie(cookieArray[i]));
      }
      

      【讨论】:

      • Thanx 但是一旦我将它发送到新活动,我如何从字符串转换回 cookie?记得把i++; 放在你的for循环中
      猜你喜欢
      • 2023-03-08
      • 2012-08-04
      • 2015-07-20
      • 2014-09-06
      • 2014-09-09
      • 1970-01-01
      • 2016-04-14
      • 2016-11-04
      • 2012-07-20
      相关资源
      最近更新 更多