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) {
}
});
&#13;
最终拦截器日志结果如下:
>
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.
答案 0 :(得分:1)
你正在混淆概念。首先,你传递的是GsonConverter:
Retrofit retrofit = new Retrofit.Builder().client(client).baseUrl(Constant.baseURL).addConverterFactory(GsonConverterFactory.create()).build();
但是你期待一个JSONObject:
Call<JSONObject> bannerResult = bannerService.getBannerList(5);
传递GsonConverter时,Retrofit正在转换为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。