1.最新的版本4.12.0,太新了,有一些方法没有了,下面的代码会报错
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
package baidu.com;
import okhttp3.*;
import org.json.JSONObject;
import java.io.*;
/**
* 需要添加依赖
* <!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
* <dependency>
* <groupId>com.squareup.okhttp3</groupId>
* <artifactId>okhttp</artifactId>
* <version>4.12.0</version>
* </dependency>
*/
class Sample {
static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
public static void main(String []args) throws IOException{
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token?client_id=4wKpC2ruQfjEdEawOHiJR0fz&client_secret=QNetnespy9cxwr3DlAcyX0aS3lUea693&grant_type=client_credentials")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
System.out.println(response.body().string());
}
}
2.版本号小一点的,4.11.0会报Companion错误
3.我试过可以使用的版本是:
<!--文心一言API依赖-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.0</version>
</dependency>