【问题标题】:how to save cookies in android app?如何在 Android 应用程序中保存 cookie?
【发布时间】:2017-09-22 13:28:12
【问题描述】:

API 在 cookie 中创建和保存数据。它在浏览器上运行良好。但该 cookie 并未保存在 android 应用程序中。总是显示一个空白数组。有人知道这件事吗? android开发人员必须添加任何库来保存cookie吗?或者是 API 方面的问题。

【问题讨论】:

  • 您必须将 cookie 存储在共享首选项中,并将其用于您想要继续的其他请求。默认不保存
  • 共享偏好就是答案
  • 那么你保存不正确。您需要修复保存它们的方式,就是这样。

标签: android api cookies


【解决方案1】:

你可以试试这个。

/**
 * Checks the response headers for session cookie and saves it if it
 * finds it.
 * 
 * @param headers
 * Response Headers.
 */
public void checkSessionCookie(Map<String, String> headers)
{
    // Config.SET_COOKIE_KEY = "Set-Cookie"
    if (headers.containsKey(Config.SET_COOKIE_KEY) && headers.get(Config.SET_COOKIE_KEY).startsWith(Config.SESSION_COOKIE))
    {
        String cookie = headers.get(Config.SET_COOKIE_KEY);
        if (cookie.length() > 0)
        {
            String[] splitCookie = cookie.split(";");
            String[] splitSessionId = splitCookie[0].split("=");
            cookie = splitSessionId[1];
            // Now here set cookie to your Preference file.
        }
    }
}

【讨论】:

    【解决方案2】:

    请参考,

    #How to store data locally in an Android app!

    在这些选项中,您可以使用#Shared Preferences 并存储用户信息并通过应用程序访问它。

    【讨论】:

      猜你喜欢
      • 2016-10-05
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多