最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Java给OkHttp加cookie出错:Cannot resolve symbol setCookiePolicy和Unknown class CookiePolicy.ACCEPT_ALL

Java crifan 1836浏览 0评论
折腾:
【未解决】OkHttp中如何获取响应中的Cookie和管理Cookie
期间,参考:
okhttp3 – Automatic cookie handling with OkHttp 3 – Stack Overflow
用代码:
试了代码:
    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’
android-cooike的使用 – 火星男孩的分享空间 – CSDN博客
CookiePolicy 原理解析 – jijianshuai的专栏 – CSDN博客
CookiePolicy (Java Platform SE 8 )
是有:
static CookiePolicy ACCEPT_ALL
的啊
Java Code Examples java.net.CookiePolicy
https://www.programcreek.com/java-api-examples/index.php?api=java.net.CookiePolicy
然后又发现:
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
报错:
Cannot resolve symbol ‘setCookiePolicy’
okhttp3 – Cannot resolve method setCookieHandler (java.net.CookieManger – Stack Overflow
https://stackoverflow.com/questions/35329869/cannot-resolve-method-setcookiehandler-java-net-cookiemanger
然后去IDEA中看源码,也是有的啊
import java.net.CookieManager;
public void setCookiePolicy(CookiePolicy cookiePolicy) {
    if (cookiePolicy != null) policyCallback = cookiePolicy;
}
然后试试代码,果然是列不出来:setCookiePolicy
感觉是和前面那些库冲突了?
java CookieManager setCookiePolicy
CookieManager (Java Platform SE 8 )
Java Tutorial – Java CookieManager .setCookiePolicy (CookiePolicy cookiePolicy)
Java Code Examples java.net.CookieManager.setCookiePolicy
https://www.programcreek.com/java-api-examples/?class=java.net.CookieManager&method=setCookiePolicy
Android中Cookie的使用 – 初心 – CSDN博客
所以现在问题转换为:
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
IntelliJ IDEA 中项目 Cannot Resolve symbol **解决方案 – _William_Cheung的博客 – CSDN博客
Intellij IDEA Cannot resolve symbol XXX 问题解决办法汇总 – lesaqiu的专栏 – CSDN博客
java – IntelliJ inspection gives “Cannot resolve symbol” but still compiles code – Stack Overflow
试试:
File | Invalidate Caches / Restart
重启后,开始重新建立索引了,此处暂时没报错了:
看看索引建立完毕后,是否还会报错。
结果问题依旧:
去build.gradle右键 同步 试试
同步成功,但问题依旧:
java – IntelliJ cannot resolve any symbols or methods – Stack Overflow
Intellij IDEA Cannot Resolve Symbol XXX问题解决方法汇总 _pc6资讯
Refresh了gradle后,问题依旧:
注意到:
CookieManager cookieManager = new CookieManager();
中的:cookieManager
有提示问题:
IntelliJ IDEA Access can be private java.net CookieManager
intellij idea – Disable Android Studio warning: “Access can be …” – Stack Overflow
java – Intellij IDEA give a ‘Access can be private’ warnning for @Bean – Stack Overflow
java – How to disable “Access can be package-package” message in IntelliJ 2016? – Stack Overflow
Intellij IDEA取消access can be private提示 – Spring Herald – CSDN博客
IntelliJ IDEA   java.net CookieManager
java.net.CookieManager.put java code examples | Codota
https://www.codota.com/code/java/methods/java.net.CookieManager/put
java.net.CookieManager Example | Examples Java Code Geeks – 2018
https://examples.javacodegeeks.com/core-java/net/cookiemanager/java-net-cookiemanager-example/
【java】使用URL和CookieManager爬取页面的验证码和cookie并保存 – jinhuazhe2013 – 博客园
还是怀疑是库有重名的?
但是注释掉别的库,还是没有正常
看到提示 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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.198 seconds, using 22.14MB memory