[英]Retrofit2 response body is null
我構建了Retrofit2以從https獲取請求,我還構建了具有OKHttpClient的日志攔截器,日志顯示了來自http get的正確結果,但是Retrofit的response.body()為空,我該如何解決該問題?
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { //打印retrofit日志Log.i("RetrofitLog","retrofitBack = "+message); } }); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .build(); Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl(Constant.baseURL).addConverterFactory(GsonConverterFactory.create()).build(); BannerService bannerService = retrofit.create(BannerService.class); Call<JSONObject> bannerResult = bannerService.getBannerList(5); bannerResult.enqueue(new Callback<JSONObject>() { @Override public void onResponse(Call<JSONObject> call, Response<JSONObject> response) { if (response.isSuccessful()) { System.out.println("----------------------\\t"+response.body()); } else { // error response, no access to resource? } } @Override public void onFailure(Call<JSONObject> call, Throwable t) { } });
最終的攔截器日志結果如下:
>
I/RetrofitLog: retrofitBack = Content-Type: application/json;charset=UTF-8
I/RetrofitLog: retrofitBack = Content-Length: 1515
retrofitBack = Connection: keep-alive
retrofitBack = Cache-Control: must-revalidate, no-store
retrofitBack = Last-Modified: Fri, 15 Jun 2018 03:00:00 GMT
retrofitBack = Pragma: no-cache
retrofitBack = X-XSS-Protection: 1; mode=block
retrofitBack = X-Frame-Options: DENY
retrofitBack = X-Content-Type-Options: nosniff
retrofitBack = X-Daa-Tunnel: hop_count=2
retrofitBack = X-NWS-LOG-UUID: 0756741f-5bc5-44ba-9b3e-82dd08ae6f95
retrofitBack = X-Cache-Lookup: Hit From Upstream
........
>
> ----------------//here is the correct out------------------
>
>
> I/RetrofitLog: retrofitBack = {"resultModel":[{"banner_id":3,"banner_title":"拾光cafe","banner_description":"合肥工業大學校內的咖啡廳,方便實惠","banner_image_name":"84c61eca-ac3c-4fa3-8146-43cce52aebc3.jpg","banner_create_time":"2018-05-31 12:54:47","banner_creater_id":1000000,"banner_href":"1000000807","enabled":1},{"banner_id":100003,"banner_title":"合肥悅方IDMall · 永輝超市","banner_description":"一個永遠都有試吃的超市","banner_image_name":"fe154125-3752-4842-907d-203e45ae51ad.jpg","banner_create_time":"2018-03-26 16:33:45","banner_creater_id":100000000,"banner_href":"http","enabled":1},{"banner_id":100002,"banner_title":"斛兵塘","banner_description":"距今已有一千七百多年歷史的一個水塘","banner_image_name":"b4f1281c-e43d-4a69-903f-5e1eb3505938.jpg","banner_create_time":"2018-03-26 16:13:13","banner_creater_id":100000000,"banner_href":"http","enabled":1},{"banner_id":100001,"banner_title":"合肥天鵝湖","banner_description":"一個形狀神似天鵝的人工湖","banner_image_name":"1c796d8d-b868-4590-ae0e-d0599ad462cf.jpg","banner_create_time":"2018-03-26 16:10:32","banner_creater_id":100000000,"banner_href":"http","enabled":1},{"banner_id":100000,"banner_title":"合肥工業大學","banner_description":"一個食堂上央視的高校","banner_image_name":"92361818-2daf-45db-9294-d36241fd243b.jpg","banner_create_time":"2018-03-22 10:05:21","banner_creater_id":1000000,"banner_href":"1000000241","enabled":1}],"resultCode":200,"resultMessage":"請求成功"}
>
>
>> ----------------//here is the correct out------------------
>
>
> .........
>
>
>
I/System.out: ---------------------- {} //here is my system.out is empty
>
>
>
> I/Choreographer: Skipped 253 frames! The application may be doing too much work on its main thread.
您正在混淆概念。 首先,您要傳遞一個GsonConverter:
Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl(Constant.baseURL).addConverterFactory(GsonConverterFactory.create()).build();
但是,然后您期望一個JSONObject:
Call<JSONObject> bannerResult = bannerService.getBannerList(5);
通過GsonConverter時,翻新將轉換為Gson,而不是JSONObject。 這意味着您應該使用適當的Gson注釋聲明自己的Java pojo。 例:
import com.google.gson.annotations.Expose;
public class Banner {
@Expose
public String oneField
@Expose
public String otherField;
}
如您所期望的那樣,您可以更改BannerService:
public interface BannerService {
@GET("/your/path/to/get/banner/list")
Call<List<Banner>> getBannerList(@Path("id") int id);
}
最后更改您的行以接收列表...
Call<List<Banner>> bannerResult = bannerService.getBannerList(5);
並替換您的Callback實例化和方法,以在期望JSONObject的位置接收List。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.