Retrofit and persistent cookie store

Deadly 提交于 2019-12-22 11:09:26

问题


What is the most simple way to implement persistent cookie store on retrofit? Now I am using this:

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

but I need cookies to be saved and restored any other time.


回答1:


You would need to build your own CookieStore implementation. The implementation would be up to you. You could choose to persistent the data into SharedPreference, flat file, JSON, XML ...

It would look something like so.

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

Then you can simply use it like so

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

Here's an SO answer which attempted to created its own SharedPreference implementation. It might help you get started.



来源:https://stackoverflow.com/questions/25293446/retrofit-and-persistent-cookie-store

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!