其他分享
首页 > 其他分享> > SpringBoot使用OkHttp

SpringBoot使用OkHttp

作者:互联网

导入依赖:

<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");
//进行业务处理
}
}
});

-----------------------------------
SpringBoot使用OkHttp
https://blog.51cto.com/u_15127509/4378289

标签:SpringBoot,JSONArray,responseStr,resJSONObject,Call,使用,OkHttp,new,Overridepublic
来源: https://www.cnblogs.com/telwanggs/p/16590848.html