晚安,
我正在使用Retrofit2使用以下依赖项发出POST请求
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
compile 'com.google.code.gson:gson:2.7'
现在我遇到的问题是 HttpLoggingInterceptor 的响应是正确的,但响应主体是空的。
这是我的代码
Retrofit2设置
public Retrofit setUpRetrofit(){
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();
return retrofit;
}
API
@POST("hngeCommerceWebservice/rest/product/categoryNew/")
Call<Data> getData(@Body UserData userData);
MainActivity:
Call<Data> dataCall = api.getData(userData);
dataCall.enqueue(new Callback<Data>() {
@Override
public void onResponse(Call<Data> call, Response<Data> response) {
int statusCode = response.code();
Log.d(TAG, response.toString() + " STRING");
Log.d(TAG, response.body().getSkuItems()+ " BODY");
Log.d(TAG, response.raw() + " RAW");
Log.d(TAG, response.message().toString());
Log.w("Response ",new Gson().toJson(response));
}
@Override
public void onFailure(Call<Data> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage() + "");
}
});
这是 Log.w的响应(“响应”,新的Gson()。toJson(响应));
{"body":{"brandFilterdata":[],"priceFilterData":[],"skuItems":[],"validCouponCode":false},"rawResponse":{"body":{"contentLength":-1,"contentType":{"mediaType":"application/json","subtype":"json","type":"application"}},"code":200,"headers":{"namesAndValues":["Server","Apache-Coyote/1.1","Content-Type","application/json","Transfer-Encoding","chunked","Date","Thu, 15 Sep 2016 19:14:36 GMT"]},"message":"OK","networkResponse":{"code":200,"headers":{"namesAndValues":["Server","Apache-Coyote/1.1","Content-Type","application/json","Transfer-Encoding","chunked","Date","Thu, 15 Sep 2016 19:14:36 GMT"]},"message":"OK","protocol":"HTTP_1_1","receivedResponseAtMillis":1473966805216,"request":{"cacheControl":{"isPrivate":false,"isPublic":false,"maxAgeSeconds":-1,"maxStaleSeconds":-1,"minFreshSeconds":-1,"mustRevalidate":false,"noCache":false,"noStore":false,"noTransform":false,"onlyIfCached":false,"sMaxAgeSeconds":-1},"headers":{"namesAndValues":["Content-Type","application/json; charset\u003dUTF-8","Content-Length","235","Host","119.81.82.197:9090","Connection","Keep-Alive","Accept-Encoding","gzip","User-Agent","okhttp/3.3.1"]},"method":"POST","tag":{"headers":{"namesAndValues":[]},"method":"POST","url":{"host":"119.81.82.197","password":"","pathSegments":["hngeCommerceWebservice","rest","product","categoryNew",""],"port":9090,"scheme":"http","url":"http://119.81.82.197:9090/hngeCommerceWebservice/rest/product/categoryNew/","username":""}},"url":{"host":"119.81.82.197","password":"","pathSegments":["hngeCommerceWebservice","rest","product","categoryNew",""],"port":9090,"scheme":"http","url":"http://119.81.82.197:9090/hngeCommerceWebservice/rest/product/categoryNew/","username":""}},"sentRequestAtMillis":1473966804371},"protocol":"HTTP_1_1","receivedResponseAtMillis":1473966805216,"request":{"headers":{"namesAndValues":["Content-Type","application/json; charset\u003dUTF-8","Content-Length","235"]},"method":"POST","tag":{"headers":{"namesAndValues":[]},"method":"POST","url":{"host":"119.81.82.197","password":"","pathSegments":["hngeCommerceWebservice","rest","product","categoryNew",""],"port":9090,"scheme":"http","url":"http://119.81.82.197:9090/hngeCommerceWebservice/rest/product/categoryNew/","username":""}},"url":{"host":"119.81.82.197","password":"","pathSegments":["hngeCommerceWebservice","rest","product","categoryNew",""],"port":9090,"scheme":"http","url":"http://119.81.82.197:9090/hngeCommerceWebservice/rest/product/categoryNew/","username":""}},"sentRequestAtMillis":1473966804371}}
但 HttpLoggingInterceptor
的正确回应