Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie not getting saved when used with kotlin and OKHTTP, while same works for Java and OKHTTPretrofit #36

Open
sidharthanil opened this issue Jun 16, 2017 · 7 comments

Comments

@sidharthanil
Copy link

Hi, I am using kotlin to do my android project. Im using this library on my other java projects on android and all of them works fine. But when I use this with kotlin the cookie is not getting saved. I want to know whether this has occured to you or do you know the work around for this issue?

I have manually checked using adb commands to read the shared prefrences. While on my java project the cokie is being set but here it isnt. Please respond fast

@franmontiel
Copy link
Owner

I`m sorry I have no info about this problem, If you wish you can track it down and let us know your discoveries

@alanum
Copy link

alanum commented Jul 5, 2017

i got the same problem ...cry!!!

@jiangecho
Copy link

got the same problem +1

@a2open
Copy link

a2open commented Aug 18, 2017

I also encountered the same problem in Kotlin

@Serhii-the-Dev
Copy link

I've also faced that problem, and in my case it was related to Kotlin object declaration. So if you also using an object as a fabric of Retrofit APIs, you can bypass the issue using by lazy delegate.
Since I came here looking for solution, maybe my example will help a next wanderer:

object HttpApi {
    lateinit private var cookiesHandler: CookieJar

    fun updateCookieJarWithContext(context: Context) {
        cookiesHandler = PersistentCookieJar(SetCookieCache(), SharedPrefsCookiePersistor(context))
    }

    private val loggingInterceptor by lazy {
        HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
    }

    private val httpClient by lazy {
        OkHttpClient.Builder().apply {
            addInterceptor(loggingInterceptor)
            cookieJar(cookiesHandler)
        }.build()
    }

    private val builder by lazy { Retrofit.Builder().addConverterFactory(JacksonConverterFactory.create()) }

    private fun <ApiInterface> createService(serviceClass: Class<ApiInterface>, apiEndpointUrl: String = "rest") = builder
            .baseUrl("https://***/$apiEndpointUrl/")
            .client(httpClient)
            .build()
            .create(serviceClass)

    val rest: HttpRestApi by lazy { createService(HttpRestApi::class.java) }
    val auth: HttpAuth by lazy { createService(HttpAuth::class.java, "auth") }
}

Also I recommend to use a dependency injection framework, like Dagger, there is an example of application context injection.

@wbinarytree
Copy link

I'm using full kotlin and meet the problem.
It turns out we don't have a persistent property always false to won't be saved in SharedPref.
After I rewrite the method saveFromResponse to force save it. It works fine.
related issue #14 #15

@tmdroid
Copy link

tmdroid commented Jul 28, 2020

@franmontiel why not just have an option to save all cookies regardless if they have max-age or expires attributes? Seems like this persistence thing is causing many people problems with your library including myself. I'm using Kotlin and building a login flow where I store some cookies from a request, navigate to a 3rd party app for logging in and then come back to my original app, and then to my surprise, some important cookies are lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants