【发布时间】:2018-04-07 09:19:46
【问题描述】:
我想在现有 Javafx 项目中使用 OkHttp 库。我正在使用 Netbeans,我已经将 OkHttp jar 导入到项目中,但我不确定如何使用。另外,是否可以只包含 lib 的一部分而不是整个 jar?无论哪种方式,我如何将其添加到我现有的项目中?我假设它是某种类型的import,但我不确定。
【问题讨论】:
我想在现有 Javafx 项目中使用 OkHttp 库。我正在使用 Netbeans,我已经将 OkHttp jar 导入到项目中,但我不确定如何使用。另外,是否可以只包含 lib 的一部分而不是整个 jar?无论哪种方式,我如何将其添加到我现有的项目中?我假设它是某种类型的import,但我不确定。
【问题讨论】:
假设including of the JAR 正常,那么可以在the site of OkHttp 上找到如何使用它的示例:
// https://raw.githubusercontent.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/GetExample.java
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class GetExample {
OkHttpClient client = new OkHttpClient();
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 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);
}
}
【讨论】: