Skip to content

Instantly share code, notes, and snippets.

@yusukezzz
Created January 29, 2016 15:25
Show Gist options
  • Save yusukezzz/5c19a395caf44ebc3c91 to your computer and use it in GitHub Desktop.
Save yusukezzz/5c19a395caf44ebc3c91 to your computer and use it in GitHub Desktop.
package net.yusukezzz.android.ldrviewer
import android.webkit.CookieManager
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import java.util.*
class AndroidCookieJar(val cookieManager: CookieManager): CookieJar {
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
for (cookie in cookies) {
cookieManager.setCookie(url.host(), cookie.toString())
}
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
val okHttpCookies = ArrayList<Cookie>()
val cookieString = cookieManager.getCookie(url.host())
if (cookieString != null) {
val cookies = cookieString.split(";").map { it.trim() }
for (cookie in cookies) {
okHttpCookies.add(Cookie.parse(url, cookie))
}
}
return okHttpCookies
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment