1.下载jar包(okio包+okhttp包)
https://repo1.maven.org/maven2/com/squareup/okio/okio/1.13.0/okio-1.13.0.jar
https://github.com/square/okhttp
2.把jar包导入eclipse
(参考链接:https://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html)
- 新建“libs”文件夹
- 把下载好的两个jar包复制到该文件夹
- 选中jar包右击选“build path–>“configure build path…”–>选中”Libraries“–>add jars…–>选中jar包,之后常规操作。(两个jar包都要按照这个步骤走)
新建一个Main类+工具类GetExample
(http://square.github.io/okhttp/)
public class Main {
public static void main(String[] args) throws IOException {
GetExample example = new GetExample();
String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
System.out.println(response);
}
}
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class GetExample {
OkHttpClient client = new OkHttpClient();
public String run(String url) throws IOException {
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
return response.body().string();
}
}
public GetExample() {
// TODO Auto-generated constructor stub
}
}
运行结果:
遇到的问题
- “java.lang.NoClassDefFoundError: okio/BufferedSource”
(参考链接:https://www.jianshu.com/p/e5a10fdb740c)
没有导入 okio包