I am using okhttp3 followed by retrofit2 and the next time cookies are lost
See original GitHub issueI am using okhttp3 followed by retrofit2 and the next time cookies are lost below is my code and
public static ClearableCookieJar cookieManager = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); public static ClearableCookieJar getCookieManager() { return cookieManager; }
`OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.connectTimeout(NETWORK_TIMEOUT, TimeUnit.SECONDS); builder.readTimeout(READ_TIMEOUT, TimeUnit.MINUTES); builder.cookieJar(TrestApplication.getCookieManager());
OkHttpClient clientOkHttp = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(clientOkHttp)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;`
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
How to retrieve cookie from response retrofit, okhttp?
Now You have cookies it's time to use it. Wherever you want to use it, just get the cookies that you saved earlier...
Read more >Retrofit 2 — Log Requests and Responses - Future Studio
This post will show you how to add and use the logging interceptor in your Android app in combination with Retrofit 2.
Read more >Using Retrofit 2.x as REST client - Tutorial - Vogella.com
In this exercise you will create a standalone REST client. ... Call; import retrofit2.http. ... To add an interceptor, you have to use...
Read more >How to handle RESTful web Services using Retrofit, OkHttp ...
If we come back to the same screen the next time, the app will load data from the database instead of making a...
Read more >Retrofit Your Exceptions Using Retrofit | Ackee blog
This post is a follow up to my previous post about mapping into domain exceptions. To quickly summarise what it's all about —...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@wangdandong with your solution the library will persist all cookies, if you do not have access to the server to configure the cookies properly can be good workaround but the real good solution is to configure correctly the cookies with an expire date so the cookies can be persisted.
A more elegant workaround when you do not have access to the server but you need to persist the cookies is to create a OkHttp Interceptor that takes the incoming cookies and add to them a expire date.
In class
PersistentCookieJar
methodsaveFromResponse
changepersistor.saveAll(filterPersistentCookies(cookies));
topersistor.saveAll(cookies);