OkHttp post json数据,Java
需要印日maven依赖:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.10</version>
</dependency>
private String post(OkHttpClient client, String url, String json) throws Exception {
final MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(json, JSON);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
final Call call = client.newCall(request);
Response response = call.execute();
return response.body().string();
}
OkHttpClient需要以单例模式获取,最好不要每次请求都new一个新的。