从WebView访问会话变量

时间:2014-01-27 09:39:57

标签: android android-webview

是否可以从Android中的webview控件访问会话变量(及其值)?

我搜索了文档但没有找到任何内容。

1 个答案:

答案 0 :(得分:-1)

您可以像这样同步webview session cookis

public static void sync(String url) {
         List<Cookie> cookies=null;
        CookieManager cookieManager = CookieManager.getInstance();
        if (cookieManager == null)
            return;

        RFC2109Spec cookieSpec = new RFC2109Spec();
        String rawCookieHeader = null;
        try {
            URL parsedURL = new URL(url);
            rawCookieHeader = cookieManager.getCookie(parsedURL.getHost());
            if (rawCookieHeader == null)
                return;
            int port = parsedURL.getPort() == -1 ? parsedURL.getDefaultPort()
                    : parsedURL.getPort();

            CookieOrigin cookieOrigin = new CookieOrigin(parsedURL.getHost(),
                    port, "/", false);
            cookies = cookieSpec.parse(new BasicHeader("set-cookie",
                    rawCookieHeader), cookieOrigin);

            System.out.println("Session Cookis : " + cookies);

        } catch (Exception e) {

        }
    }

怎么打电话?来自webview

webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                sync(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });