私は次のことをしています:
私の
Launcher
活動では、次のようonCreate
にデフォルトを初期化した後:CookieManager
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()