折腾:
【未解决】OkHttp中如何获取响应中的Cookie和管理Cookie
期间,参考:
用代码:
试了代码:
CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new LoggingInterceptor()) .addInterceptor(new ResponseInterceptor()) // .cookieJar(cookieJar) .cookieJar(new JavaNetCookieJar(cookieManager)) .build();
结果提示:
Unknown class: ‘CookiePolicy.ACCEPT_ALL’
是有:
static CookiePolicy ACCEPT_ALL
的啊
Java Code Examples java.net.CookiePolicy
然后又发现:
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
报错:
Cannot resolve symbol ‘setCookiePolicy’
okhttp3 – Cannot resolve method setCookieHandler (java.net.CookieManger – Stack Overflow
然后去IDEA中看源码,也是有的啊
import java.net.CookieManager;
public void setCookiePolicy(CookiePolicy cookiePolicy) { if (cookiePolicy != null) policyCallback = cookiePolicy; }
然后试试代码,果然是列不出来:setCookiePolicy
感觉是和前面那些库冲突了?
java CookieManager setCookiePolicy
Java Code Examples java.net.CookieManager.setCookiePolicy
所以现在问题转换为:
IntelliJ IDEA中,虽然已经导入了:
import java.net.CookieManager; import java.net.CookiePolicy;
且 Command+鼠标点击 都可以进入源码,看到有对应的:
CookieManager的setCookiePolicy
和
CookiePolicy的ACCEPT_ALL
但是还是很诡异的对于代码:
CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
提示:
Cannot resolve symbol
intellij idea Cannot resolve symbol
试试:
File | Invalidate Caches / Restart
重启后,开始重新建立索引了,此处暂时没报错了:
看看索引建立完毕后,是否还会报错。
结果问题依旧:
去build.gradle右键 同步 试试
同步成功,但问题依旧:
Refresh了gradle后,问题依旧:
注意到:
CookieManager cookieManager = new CookieManager();
中的:cookieManager
有提示问题:
IntelliJ IDEA Access can be private java.net CookieManager
IntelliJ IDEA java.net CookieManager
java.net.CookieManager.put java code examples | Codota
java.net.CookieManager Example | Examples Java Code Geeks – 2018
还是怀疑是库有重名的?
但是注释掉别的库,还是没有正常
看到提示 multiple choice后:
用了Alt+Enter,可以进入选项
import com.sun.webkit.network.CookieManager;
然后结果肯定还是不行的
因为里面是没有setCookiePolicy
后来把代码换到别的位置,就没有报错了:
所以想办法把class中全局的私有变量,去整合到class的初始化中。
改变之前的代码:
public class EmulateBaiduLogin { // OkHttpClient client = new OkHttpClient(); // ClearableCookieJar cookieJar = // new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(context)); private CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); // CookieJar cookieJar = new CookieJar(); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new LoggingInterceptor()) .addInterceptor(new ResponseInterceptor()) // .cookieJar(cookieJar) .cookieJar(new JavaNetCookieJar(cookieManager)) .build(); String UserAgent_Mac_Chrome = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"; // ResponseBody run(String url) throws IOException { Response run(String url) throws IOException { Request request = new Request.Builder() .url(url) .header("User-Agent", UserAgent_Mac_Chrome) .header("Host", "www.baidu.com") .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8") // .header("Accept-Encoding", "gzip, deflate, br") .header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") .header("Cache-Control", "no-cache") .build(); try (Response response = client.newCall(request).execute()) { // return response.body().string(); // return response.body(); return response; } }
变成:
先去解决:
【已解决】Java中如何类的初始化
以及:
【已解决】Java中如何实现字符串的枚举
【总计】
之前的代码:
public class EmulateBaiduLogin { CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new LoggingInterceptor()) .addInterceptor(new ResponseInterceptor()) // .cookieJar(cookieJar) .cookieJar(new JavaNetCookieJar(cookieManager)) .build();
始终报错
Cannot resolve symbol setCookiePolicy和Unknown class CookiePolicy.ACCEPT_ALL
的原因是:
这部分的变量的初始化放错地方了
-》错误的放到了类的全局的变量的地方了
-》其实根据此处的只是第一次初始化才用得到的情况,应该改为:
放到构造函数中,变成:
public class EmulateBaiduLogin { private static CookieManager cookieManager; private static OkHttpClient httpClient; EmulateBaiduLogin(){ LoggingInterceptor loggingInterceptor = new LoggingInterceptor(); ResponseInterceptor responseInterceptor = new ResponseInterceptor(); JavaNetCookieJar javaNetCookieJar = new JavaNetCookieJar(cookieManager); cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); httpClient = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .addInterceptor(responseInterceptor) .cookieJar(javaNetCookieJar) .build(); }
即可解决错误了。
转载请注明:在路上 » 【已解决】Java给OkHttp加cookie出错:Cannot resolve symbol setCookiePolicy和Unknown class CookiePolicy.ACCEPT_ALL