Persistent Cookie store using okhttp 2 on Android

后端 未结 2 1879
后悔当初
后悔当初 2020-12-12 17:37

In my Android app, I am trying to switch from android-async-http to okhttp which supports async networking sind version 2.0. While the former ships

2条回答
  •  攒了一身酷
    2020-12-12 18:07

    You can set your CookieStore in the OkHttp client with the following code:

    OkHttpClient client = new OkHttpClient();
    client.setCookieHandler(new CookieManager(
                           new PersistentCookieStore(getApplicationContext()),
                           CookiePolicy.ACCEPT_ALL));
    

    I made a gist with my implementation of a persistent CookieStore trying to improve janoliver's answer in two points:

    1. Based on this answer the URI should not be used if the domain and path attributes of the cookies are available.
    2. In SerializableHttpCookie class the HttpCookie httpOnly attribute is not serialized (it does not have accessors nor mutators). My solution was to use reflection to be able to access this private attribute.

提交回复
热议问题