Android WebView HTTP Cookies在API 21中不起作用 [英] Android WebView HTTP Cookies not working in API 21

查看:118
本文介绍了Android WebView HTTP Cookies在API 21中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用WebView和HTTP cookie的Android应用程序。此应用程序可在运行API 19或更低版本的Android设备上运行。 API 21不会保存http cookie以供以后参考。

I have an Android application that uses WebView and HTTP cookies. This application works on Android devices running API 19 or below. API 21 is not saving the http cookie for later reference.

Android WebView代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_token);
    WebView mWebView = (WebView) findViewById(R.id.activity_main_webView1);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return false;
        }
    });
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setGeolocationEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setDatabaseEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    mWebView.loadUrl("file:///android_asset/index.html");
}

Android Manifest

uses-sdk android:minSdkVersion="16" android:targetSdkVersion="19"
uses-permission android:name="android.permission.INTERNET"

服务器端代码创建cookie:

Response.Cookies("mycookie")("myvalue") = "123456789"
Response.Cookies("mycookie").Expires = Date() + 10
Response.Cookies("mycookie").Secure = True

服务器端代码读取cookie:

Response.Write Request.Cookies("mycookie")("myvalue")




  • 这将在WebView中的API 21上返回空白值

  • 在API 19或更低版本上运行时,我可以读写cookie没问题。我正在使用cookie,就像访问任何使用cookie的网页一样。任何帮助将不胜感激。

    When this runs on API 19 or below I can read/write cookies no problem. I am using cookies as you would with visiting any web page that uses cookies. Any help would be appreciated.

    推荐答案

    API 21或Lollipop要求将此添加到您的APP

    API 21 or Lollipop requires this to be added to your APP

    if (Build.VERSION.SDK_INT >= 21) {
        // AppRTC requires third party cookies to work
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(mWebView, true);
    }
    

    再次使用!

    这篇关于Android WebView HTTP Cookies在API 21中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆