參考文章:
- https://www.cnblogs.com/hongdada/p/9259965.html
- https://blog.csdn.net/qq_41890954/article/details/113994681
導入依賴:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
調用POST方法:
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
Request request = new Request.Builder()
.url(dataAddr)
.post(RequestBody.create(mediaType, reqJsonStr))
.build();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
log.error("onFailure: {} ",e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//先使用String進行接收
String responseStr = response.body().string();
log.info("responseStr: "+responseStr);
JSONObject resJSONObject = JSON.parseObject(responseStr);
if (resJSONObject.get("code").equals(200)) {
JSONArray jsonArray = (JSONArray) resJSONObject.get("data");
//進行業務處理
}
}
});