设置cookie android webview

时间:2015-02-26 08:26:05

标签: android cookies android-webview

我想在android中的webview中设置cookie,而我没有记录用户。用户正确登录到Android应用程序。这是我的代码:

            WebView webview = new WebView(this); 
            setContentView(webview);
            final WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setAppCacheEnabled(true);
            settings.setBuiltInZoomControls(true);
            settings.setPluginState(WebSettings.PluginState.ON);
            settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

            webview.setWebChromeClient(new WebChromeClient());


            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            SharedPreferences prefs = getSharedPreferences(
                    "cookie", Context.MODE_PRIVATE);


            String cookie = cookieManager.getCookie(url);

            String[] cookies = cookie.split(";");

            final HashMap<String, String> cookieStrings = new HashMap<String, String>();
            for (String cook : cookies) {
                String[] cs = cook.split("=");
                cookieStrings.put(cs[0], cs[1]);
            }

            CookieSyncManager.createInstance(this);
            cookieManager.removeSessionCookie();
            CookieSyncManager.getInstance().sync();

            webview.loadUrl(url,cookieStrings);

0 个答案:

没有答案