franmontiel / PersistentCookieJar

A persistent CookieJar implementation for OkHttp 3 based on SharedPreferences.
Apache License 2.0
1.8k stars 247 forks source link

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

Open sidharthanil opened 7 years ago

sidharthanil commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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

jiangecho commented 7 years ago

got the same problem +1

a2open commented 7 years ago

I also encountered the same problem in Kotlin

Serhii-the-Dev commented 7 years ago

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 commented 7 years ago

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 commented 4 years ago

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