-
强哥,我想用一个CookieStore来共享Net客户端(准确的说是okhttp3)以及WebView的cookie信息,但入口不知道该怎么写了?求帮指导下,代码如下:
package com.bytedance.mbesx.api.cookie
import android.webkit.CookieManager
import java.net.HttpCookie
import java.net.URI
interface CookieStore {
fun getCookie(url: String): List<String>
fun setCookie(url: String, cookies: List<String>)
}
class SharedCookieStore constructor(
private val javaNetCookieStore: java.net.CookieStore,
private val cookieManager: CookieManager,
) : CookieStore {
override fun getCookie(url: String): List<String> {
return javaNetCookieStore.get(URI(url))
.map { it.toString() }
}
override fun setCookie(url: String, cookies: List<String>) {
cookies.map { cookie ->
HttpCookie.parse(cookie).forEach { httpCookie ->
javaNetCookieStore.add(URI(url), httpCookie)
}
cookieManager.setCookie(url, cookie)
}
}
}
package com.bytedance.mbesx.api.cookie
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
class CookieJarImpl(
private val sharedCookieStore: CookieStore,
) : CookieJar {
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
sharedCookieStore.setCookie(
url = url.toString(),
cookies = cookies.map { it.toString() },
)
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
return sharedCookieStore.getCookie(url.toString())
.mapNotNull { Cookie.parse(url, it) }
}
}
import com.bytedance.mbesx.api.cookie.CookieJarImpl
import com.bytedance.mbesx.api.cookie.SharedCookieStore
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initNet()
}
/**
* 初始化网络请求客户端
*/
private fun initNet (){
NetConfig.initialize(Api.HOST,this) {
//超时设置
connectTimeout(30, TimeUnit.SECONDS)
readTimeout(30, TimeUnit.SECONDS)
writeTimeout(30, TimeUnit.SECONDS)
// 添加持久化Cookie管理
// cookieJar(CookieJarImpl()) 这里改如何实例化java.net.cookieStore
// 响应序列化
setConverter(SerializationConverter())
}
}
} 入口的实例化,我不知道该咋写了?本人刚接触安卓不久,请教下 |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
看不太懂你抄的什么东西, CookieJar这个接口是Okhttp设计的不是我
|
Beta Was this translation helpful? Give feedback.
-
这里我推荐的做法 |
Beta Was this translation helpful? Give feedback.
-
已经解决 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()) {
val cookieHeaders = cookiesString.split(";".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray()
val cookies: MutableList<Cookie> = ArrayList(cookieHeaders.size)
for (header in cookieHeaders) {
cookies.add(Cookie.parse(url, header)!!)
}
return cookies
}
return emptyList()
}
} |
Beta Was this translation helpful? Give feedback.
-
OkHttp是Android的网络请求生态, 各种需求你都可以网上查到, 这也是本项目优势之一, 完全兼容OkHttp的Api |
Beta Was this translation helpful? Give feedback.
已经解决 cookieJar 传入这个就可以