【问题标题】:How do I include OkHttp into a project not using Gradle or Maven?如何将 OkHttp 包含到不使用 Gradle 或 Maven 的项目中?
【发布时间】:2018-04-07 09:19:46
【问题描述】:

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

【问题讨论】:

    标签: java netbeans


    【解决方案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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-14
      • 1970-01-01
      • 2018-07-11
      • 2014-02-05
      • 1970-01-01
      • 2020-11-14
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多