折腾:
【未解决】Mac的Java中使用OkHttp去进行基本的网络请求
期间,想要去试试如何使用OkHttp,然后参考官网的:
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
说是需要再去下载和导入:okio.jar

去导入




【总结】
此处IntelliJ IDEA导入了OkHttp的jar包后,去运行代码,结果:
OkHttpClient client = new OkHttpClient();
报错:
Exception in thread “main” java.lang.NoClassDefFoundError: okio/BufferedSource
原因是:
缺少了对应的,OkHttp所依赖的库:okio
所以去:
下载到对应的:
okio-2.1.0.jar
再去导入IDEA后,即可。
转载请注明:在路上 » 【已解决】java中调用OkHttp出错:Exception in thread “main” java.lang.NoClassDefFoundError: okio/BufferedSource