question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I am using okhttp3 followed by retrofit2 and the next time cookies are lost

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
franmontielcommented, Feb 13, 2017

@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.

9reactions
wangdandongcommented, Feb 12, 2017

In class PersistentCookieJar method saveFromResponse change persistor.saveAll(filterPersistentCookies(cookies)); to persistor.saveAll(cookies);

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found