一、请求参数
参数名称 | 参数值 |
---|
Content-Type | application/json |
二、Body
三、实例展示
Map map = new HashMap();
map.put("name","彭老希");
map.put("age","19");
String jsonPost = JSON.toJSONString(map);
DishLog.i("postRequestJson",jsonPost);
MediaType postrequestjsontype = MediaType.parse("application/json; charset=utf-8");
String url = "请求的post链接";
OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(postrequestjsontype,jsonPost);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
if (response.isSuccessful()){
Log.i("onResponse",response.body().string());
}
}
});