1

我正在执行以下操作:

  • 在我的Launcher活动中,在onCreateCookieManager像这样初始化默认值之后:

    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        CookieSyncManager.createInstance(ctx);
    }
    
    cookieManager.setAcceptCookie(true);
    cookieManager.setAcceptFileSchemeCookies(true);
    
  • 之后我在做这个...

    CookieHandler.setDefault(new WebkitCookieManagerProxy());
    
  • ...我在哪里扩展CookieManager并这样做:

    public class WebkitCookieManagerProxy extends CookieManager {
    private static final String TAG = WebkitCookieManagerProxy.class.getName();
    private XWalkCookieManager crosswalkCookieManager;
    
    public WebkitCookieManagerProxy() {
        this(null, null);
    }
    
    /* package */ WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy) {
        super(null, cookiePolicy);
    
        this.crosswalkCookieManager = new XWalkCookieManager();
    
    }
    
    // java.net.CookieManager overrides
    
    @Override
    public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {
        // make sure our args are valid
        if ((uri == null) || (responseHeaders == null)) return;
    
        // save our url once
        String url = uri.toString();
    
        // go over the headers
        for (String headerKey : responseHeaders.keySet()) {
            // ignore headers which aren't cookie related
            if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie")))
                continue;
    
            // process each of the headers
            for (String headerValue : responseHeaders.get(headerKey)) {
                this.crosswalkCookieManager.setCookie(url, headerValue);
            }
        }
    }
    
    @Override
    public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException {
        // make sure our args are valid
        if ((uri == null) || (requestHeaders == null))
            throw new IllegalArgumentException("Argument is null");
    
        // save our url once
        String url = uri.toString();
    
        // prepare our response
        Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
    
        // get the cookie
        String cookie = null;
        if (this.crosswalkCookieManager.hasCookies()) {
            cookie = this.crosswalkCookieManager.getCookie(url);
        }
    
        // return it
        if (cookie != null) {
            res.put("Cookie", Arrays.asList(cookie));
        }
        return res;
    }
    
    @Override
    public CookieStore getCookieStore() {
        // we don't want anyone to work with this cookie store directly
        throw new UnsupportedOperationException();
    }
    

    }

我的问题是,当它进入put()上面的方法时,它会崩溃并出现异常。对管理器的任何调用都会使应用程序崩溃,例如调用hasCookies()getCookies()

4

0 回答 0