gpt4 book ai didi

android - 在 Android WebView 中设置启用 Cookie

转载 作者:太空狗 更新时间:2023-10-29 15:00:43 26 4
gpt4 key购买 nike

我正在尝试为 Android webview 启用 cookie,但它对我不起作用这是我试过的

CookieManager cookieManager = CookieManager.getInstance(); 
CookieSyncManager.createInstance(this);

webView = (WebView) findViewById(R.id.webView);
cookieManager.setAcceptCookie(true);
cookieManager.acceptCookie();
CookieSyncManager.getInstance().startSync();
WebSettings settings = webView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
settings.setDefaultZoom(ZoomDensity.FAR);
settings.setJavaScriptEnabled(true);
settings.setBuiltInZoomControls(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setAppCacheEnabled(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);

这是 JS

function getCookie(cname) {

var name = cname + "=";
var ca = document.cookie.split(';');
alert(ca);
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}

更新

我是这样设置cookie的

function setCookie(cname, cvalue) {

var domain = document.domain;
var currentURL = document.URL;
if (currentURL.substr(0, 4) == 'file') {
alert("in offline cookie set");
document.cookie = cname + "=" + cvalue + "; path=/;" + " domain=file://";
}
else{
document.cookie = cname + "=" + cvalue + "; path=/;" + " domain=" + domain;
}
}

最佳答案

我明白了,您正在尝试设置 file://cookie。请在创建 WebView 或访问 CookieManager 之前尝试此方法:http://developer.android.com/reference/android/webkit/CookieManager.html#setAcceptFileSchemeCookies(boolean)

请阅读此方法的文档!

关于android - 在 Android WebView 中设置启用 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27143407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com