有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带Gradle的javaokhttp

我有一个Java8项目,需要创建一个API post调用。 该项目使用gradle。 我认为OKHTTP是一个很好的解决方案,但是我不知道在build gradle类中放什么 在一些网站上,只需要把compile 'com.squareup.okhttp:okhttp:2.5.0' 在OK Http网站上,他们说

compileKotlin {
  kotlinOptions {
    jvmTarget = "1.8"
  }
}
compileTestKotlin {
  kotlinOptions {
    jvmTarget = "1.8"
  }
}

compileJava {
  sourceCompatibility = JavaVersion.VERSION_1_8
  targetCompatibility = JavaVersion.VERSION_1_8
}

安卓 {
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
} 

我不需要安卓,只想发布到服务器并在java应用程序中获得响应(我创建了发布数据的方法)。(我将在selenium中使用它,该项目是纯java的) 我找到的所有示例都是版本3。x、 所以我想创建对版本3的依赖关系。 (我不知道第3版和第4版之间有什么区别,但我想发布json/应用程序并获得响应)

我是gradle的新手,如果我需要的不仅仅是向gradle build添加行,有人能澄清一下吗

请有人一步一步地告诉我,在gradle build中,什么是唯一需要的东西


共 (1) 个答案

  1. # 1 楼答案

    如果您的项目是简单的Java8项目,那么您可以从上面的示例中跳过Kotlin和Android配置。只需在build.gradle中导入deokhhtp依赖项

    plugins{
        id "java"
    }
    dependencies{
        implementation 'com.squareup.okhttp3:okhttp:3.14.6'
        // for v4.x :
        // implementation 'com.squareup.okhttp3:okhttp:4.3.1'
    
    }
    

    并使用它:

    import okhttp3.OkHttpClient;
    
    public class Main {
    
        public static void main(String[] args){
            OkHttpClient client = new OkHttpClient();
            // use the http client....
    
        }
    }