最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】java中调用OkHttp出错:Exception in thread “main” java.lang.NoClassDefFoundError: okio/BufferedSource

Java crifan 23浏览 0评论
折腾:
【未解决】Mac的Java中使用OkHttp去进行基本的网络请求
期间,想要去试试如何使用OkHttp,然后参考官网的:
OkHttp
http://square.github.io/okhttp/
-》
去用代码:
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class EmulateLoginBaidu {
    OkHttpClient client = new OkHttpClient();

    ResponseBody run(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
//            return response.body().string();
            return response.body();
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println("Try Emulate Login Baidu");

        String baiduUrl = "
http://www.baidu.com";

        EmulateLoginBaidu emulateLogin = new EmulateLoginBaidu();
        ResponseBody respBody = emulateLogin.run(baiduUrl);
        System.out.println("respBody=" + respBody);
    }
}
结果报错:
okhttp Exception in thread “main” java.lang.NoClassDefFoundError: okio/BufferedSource
java.lang.NoClassDefFoundError in 3.5.0 · Issue #3065 · square/okhttp
说是需要再去下载和导入:okio.jar
java.lang.NoClassDefFoundError: okio.Buffer 错误的解决方法 – XWdoor – CSDN博客
square/okio: A modern I/O API for Java
 the latest JAR 
-》
使用okhttp3出现java.lang.NoClassDefFoundError: okio.Buffer的错误怎么办? – wxbrissdomar的博客 – CSDN博客
okhttp3出现java.lang.NoClassDefFoundError: okio.Buffer的错误怎么办? – 一路奔跑94的博客 – CSDN博客
[1.8.9] OkHttp crushing the client? – Modder Support – Minecraft Forge Forums
去导入
【总结】
此处IntelliJ IDEA导入了OkHttp的jar包后,去运行代码,结果:
OkHttpClient client = new OkHttpClient();
报错:
Exception in thread “main” java.lang.NoClassDefFoundError: okio/BufferedSource
原因是:
缺少了对应的,OkHttp所依赖的库:okio
所以去:
square/okio: A modern I/O API for Java
下载到对应的:
okio-2.1.0.jar
再去导入IDEA后,即可。

转载请注明:在路上 » 【已解决】java中调用OkHttp出错:Exception in thread “main” java.lang.NoClassDefFoundError: okio/BufferedSource

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
19 queries in 0.095 seconds, using 9.31MB memory