【问题标题】:Why does cookie get lost when I close my Android application?为什么当我关闭我的 Android 应用程序时 cookie 会丢失?
【发布时间】:2014-08-16 00:14:08
【问题描述】:

我正在开发一个 Android 应用程序,我对此有点陌生。我目前正在尝试做的是保持用户登录。要登录,我必须向 API 发出请求并发送用户电子邮件和密码,这将返回一个我管理得很好的 JSONObject。我有一个 CookieStore 变量来获取 cookie 并将它们存储到我的 SharedPreferences 中。

我刚刚意识到如果我关闭应用程序,我的 cookie 会丢失,即使应用程序恢复,我也需要继续向 API 发出请求,因为用户已经登录。我试图在我的一项活动的 onResume() 方法中“恢复”cookie,但这并不像我想要的那样工作。如果我在恢复我的应用程序后尝试使用 CookieStore.getCookies() 获取 cookie,则该列表为 null

有人告诉我,我可以使用 loopj 的 AsynHttpClient 并使用 PersistentCookieStore 管理我的 cookie,但这不会让我遇到同样的问题吗?每次我恢复我的应用程序时,我都会失去 PersistentCookieStore 实例的价值,对吧?

所以我的问题是:

如何恢复 cookie 以保持它们的持久性并让我能够继续向 API 发出请求?

希望任何人都可以帮助我。

提前致谢。

【问题讨论】:

  • 使用 sharedpreferences 存储它应该可以工作。您可以在需要时检索它。
  • 是的,但事实并非如此。问题是如果我关闭应用程序并尝试向 API 发出新请求,我无法告诉 API 我是同一用户,因为我丢失了 cookie。我应该在提出请求的那一刻从共享偏好中恢复它们吗?我需要 cookie 的哪些信息?名称、值、域、路径和版本?
  • sharepreferences 将起作用。检索 cookie 并将其发送到服务器

标签: android cookies


【解决方案1】:

你有两个选择:

选项 1 - 如果使用 SharedPreferences 和 HttpUrlConnection:您需要在使用 HttpUrlConnection 时手动从 SharedPreferences 中检索 cookie 并将 cookie 添加到每个请求中。

选项 2 - 如果使用 loopj AsyncHttpClient:根据 loopj 文档,您必须创建一个 PersistentCookieStore 实例并将其添加到您的 AsyncHttpClient 每次重新启动您的应用程序时,就像这样

AsyncHttpClient myClient = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
myClient.setCookieStore(myCookieStore);

“this”是一个上下文。

【讨论】:

  • 谢谢!我最终使用了 loopj AsyncHttpClient,它工作得很好:)
【解决方案2】:

您如何在 SharedPreferences 中存储/恢复 cookie?你能分享一些代码吗? 你也可以尝试调试它并告诉我们它有什么问题吗?它不会存储到 SharedPreferences 中吗?是否无法恢复它们?

在将 CookieStore 存储/恢复到 SharedPreferences 之前,您需要先用它创建一个 Serializable 对象,例如 ArrayList<HashMap<String, String>

类似:

public static void storeCookies(Context context, CookieStore cookieStore) {
  ArrayList<HashMap<String, String> cookies = new ArrayList<HashMap<String, String>();
  for (Cookie cookie : cookieStore.getCookies()) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("name", cookie.getName());
    map.put("path", cookie.getPath());
    map.put("domain", cookie.getDomain());
    map.put("expiry_date", cookie.getExpiryDate());
    map.put("ports", cookie.getPorts());
    map.put("value", cookie.getValue());
    map.put("version", cookie.getVersion());
    ...
    // Add all the fields you want to save
    cookies.add(map);
  }

  SharedPreferences.Editor editor = context.getSharedPreferences().edit();
  editor.putSerializable("cookies", cookies).apply();
}

public static void addCookiesToStore(Context context, CookieStore cookieStore) {
  List<Map<String, String>> cookies = (ArrayList<HashMap<String, String>>) context.getSharedPreferences().getSerializable("cookies");

  for (Cookie map : cookieStore.getCookies()) {
    Cookie cookie = new BasicClientCookie(map.getName(), map.getValue());
    cookie.setPath(map.get("path));
    ...
    // Add all the fields you want to restore
    cookieStore.add(cookie);
  }
}

【讨论】:

    猜你喜欢
    • 2016-04-06
    • 2019-10-03
    • 2020-12-10
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多