android service操作getCookies报错

解决方案是在getCookies之前执行下面语句

CookieSyncManager.createInstance(context);

很奇怪的是在activity中不需要执行这句也能获取cookie

查看官网文档,CookiesSyncManager已经不推荐使用了,

原话如下:

This class was deprecated in API level 21.
The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager. To manually force a sync you can use the CookieManager method flush() which is a synchronous replacement for sync().

大概意思是sdk api21以后WebView会自动根据需要异步cookie,你不需要手动设置,如果需要强制异步的话可以使用CookieManager.flush()方法。