Skip to content

指导下 Net 与 WebView 自动共享cookie #182

Answered by Aaronphy
Aaronphy asked this question in Q&A
Discussion options

You must be logged in to vote

已经解决 cookieJar 传入这个就可以

import android.webkit.CookieManager
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl


class WebViewCookieHandler : CookieJar {
    private val mCookieManager = CookieManager.getInstance()
    override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
        val urlString = url.toString()
        for (cookie in cookies) {
            mCookieManager.setCookie(urlString, cookie.toString())
        }
    }

    override fun loadForRequest(url: HttpUrl): List<Cookie> {
        val urlString = url.toString()
        val cookiesString = mCookieManager.getCookie(urlString)
        if (cookiesString != null && !cookiesString.isEmpty()) {
    …

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Aaronphy
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants