androidokhttpandroid-cookiemanager

CookieManager is abstract, cannot be instantiated. Why?


OkHttpClient client = new OkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

throws "CookieManager is abstract, cannot be instantiated"

I'm importing android.webkit.CookieManager and everything seems fine from the documentation side.


Solution

  • Because the cookiemanager manages all the cookies, you can not create intances of it. That would mean one instance got different cookies that another. The cookiemanager is therefor a so called singleton. Use CookieManager cookieManager = CookieManager.getInstance() You can look it up here