如何将OkHttp包含到不使用Gradle或Maven的项目中?

时间:2018-04-07 09:19:46

标签: java netbeans

我想在现有的Javafx项目中使用OkHttp库。我正在使用Netbeans,我已经将OkHttp jar导入到项目中,但我不确定如何使用。另外,是否可以仅包含lib的一部分而不是整个jar?无论哪种方式,如何将其添加到我现有的项目?我假设它有某种import,但我不确定。

1 个答案:

答案 0 :(得分:1)

假设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);
  }
}
相关问题