เมื่อฉันใช้ okhttp3 เวอร์ชัน 4.7.2 เพื่อสร้างคำขอ json ด้วย api ที่สาม ฉันจะได้รับการตอบสนอง 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