我正在做以下事情:
在我的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()