当我使用okhttp3 ver 4.7.2创建带有第三个api的json请求时,我将收到响应204。但是当我使用asyncHttpClient创建相同的json请求时,我将收到正确的数据。
这是我使用okhttp3的代码:
MediaType JSON = MediaType.get("application/json");
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient client = new OkHttpClient().newBuilder().addInterceptor(interceptor).build();
RequestBody body = RequestBody.create("{}", JSON);
Request request = new Request.Builder()
.url(URL).post(body).build();
System.out.println("Start");
okhttp3.Response response = client.newCall(request).execute();
System.out.println(response.body().string()); // null
System.out.println(response.code()); // 204
这是我使用asyncHttpClient的代码:
ListenableFuture<org.asynchttpclient.Response> whenResponse = client
.preparePost(URL)
.addHeader("Content-Type", "application/json").setBody("{}").execute();
org.asynchttpclient.Response response = whenResponse.get();
System.out.println(response.getResponseBody()); // Correct response body
System.out.println(response.getStatusCode()); // 200
答案 0 :(得分:0)
由okhttp发送的请求json的内容类型为application / json; charset = utf-8,第三个api只允许application / json。所以他们的回应是204。