1

I using CookieManger with okHttp and i don't know why its saying:

Cannot resolve setCookieMethod. I googled its a valid method and the CookieManger and OkHttp is Imported in the project.

OkHttpClient client = new OkHttpClient();
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);        
client.setCookieHandler(cookieManager);
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
Skyyy
  • 1,539
  • 2
  • 23
  • 60
  • Try to set `CookieHandler.setDefault(cookieManager);` after the cookieManager initialization. – haihui Feb 11 '16 at 04:41
  • Is this message being thrown by your IDE. Its possible that you are user older version of the library that doesn't have the method – Balaji Katika Feb 11 '16 at 06:34

1 Answers1

4

OkHttp has a new CookieJar interface!

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cookieJar(cookieJar);
OkHttpClient client = builder.build();

where cookieJar is an instance of okhttp3.CookieJar.

If you really like java.net.CookieManager, you can use Jesse's JavaNetCookieJar: https://github.com/square/okhttp/blob/master/okhttp-urlconnection/src/main/java/okhttp3/JavaNetCookieJar.java

CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieJar cookieJar = new JavaNetCookieJar(cookieManager);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.cookieJar(cookieJar);
OkHttpClient client = builder.build();
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Eric Cochran
  • 8,414
  • 5
  • 50
  • 91
  • For the gradle dependencies, one can add the following: `compile 'com.squareup.okhttp3:okhttp:3.1.2'` and `compile 'com.squareup.okhttp3:okhttp-urlconnection:3.1.2'` – Moony Chou Feb 25 '16 at 18:26
  • 1
    @MoonyChou Yeah, or you can remove the urlconnection dependency and just copy-paste only the `JavaNetCookieJar` if that's all you need. – Eric Cochran Feb 25 '16 at 18:29