Android WebView加载不到cookie

以下配置根据需求酌情添加,建议逐个试验,cookie操作不是内存操作,建议修改配置后卸载app再重新运行防止缓存影响测试结果。

1.设置应用程序的 WebView 实例是否应发送并接受 Cookie

java 复制代码
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);

2.设置支持js,cookie一般都是通过js写入

java 复制代码
webView.getSettings().setJavaScriptEnabled(true);

3.在Android 4.4及以下的系统,webview是默认接收第三方cookie的。但是,从android 5.0开始,webview默认是不接受第三方cookie的

java 复制代码
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { 
     CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);
}

4.允许跨域

java 复制代码
// 允许跨域请求
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    webSettings.setAllowUniversalAccessFromFileURLs(true);
}else {
    // 对于低于API 16的版本,使用如下方式
    webSettings.setAllowUniversalAccessFromFileURLs(false);
}
相关推荐
fellow9917 小时前
DSH Desktop - Android Edition
android·agent·deepseek·harness·dsh
Carson带你学Android17 小时前
Android Studio Rabbit 2 发布:除了Gemini,40+ AI Agent 正式入驻!
android·ai编程
三少爷的鞋1 天前
Java 线程自上而下,Kotlin 协程自下而上
android
好的佩奇1 天前
Dart 之增强枚举
android·flutter·dart
梦影_1 天前
MySQL进阶教程(五)—— SQL调优(下)
android·大数据·数据库·后端·sql·mysql·database
好的佩奇1 天前
蓝牙之无线电与通信距离
android·flutter·蓝牙
vilya1 天前
我把一套完整的 Python Agent 运行时塞进手机 APK:依赖闭包、Rust 轮子和三个真机坑
android
呆呆敲代码的小Y2 天前
【实战精选面试题】C# 面试题详细解答,按难易程度排序
android·java·游戏·c#·游戏开发·unity面试题·c#面试题
思想觉悟2 天前
SEAndroid实战
android·linux·c++
思想觉悟2 天前
Aosp开发磨刀石之ADB速查
android·c++