【问题标题】:Retrofit and persistent cookie store改造和持久化 cookie 存储
【发布时间】:2014-10-07 05:49:42
【问题描述】:

在改造时实现持久 cookie 存储的最简单方法是什么?现在我正在使用这个:

    cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);

但我需要保存 cookie 并在其他时间恢复。

【问题讨论】:

    标签: android cookies retrofit


    【解决方案1】:

    您需要构建自己的CookieStore 实现。实施将取决于您。您可以选择将数据持久化为 SharedPreference、平面文件、JSON、XML ...

    它看起来像这样。

    public class MyCookieStore implements CookieStore {
        // Implementation goes here
    }
    

    那么你就可以这么简单地使用它了

    CookieManager cookieManager = new CookieManager(new MyCookieStore(), CookiePolicy.ACCEPT_ALL);
    CookieHandler.setDefault(cookieManager);
    

    这是一个 SO answer,它试图创建自己的 SharedPreference 实现。它可能会帮助您入门。

    【讨论】:

    • 改造不使用 Java.net 的默认 cookie 管理器吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2021-01-15
    • 2015-12-09
    相关资源
    最近更新 更多