【问题标题】:Retrofit response.body() is null, but API JSON visible in Logcat改造 response.body() 为空,但 API JSON 在 Logcat 中可见
【发布时间】:2017-01-12 15:22:42
【问题描述】:

我的 Android Java REST API 代码与另一个使用 JSON 对象层次结构(又名 List-bracket-object1-bracket object2)的 REST API 完美配合。现在我转向了一个简化的 API(googlemaps 时区——只有一个包含 5 个字段的简单对象),但我不知道如何将 google 数据放入我的对象中。

我收到状态码 200,我可以在我的 logcat 中看到 googlemaps 输出/JSON,但我的对象为空。我读了这篇文章“0 Retrofit 2: response.body() is null, but status code is 200 2”,我确信我有同样的问题,但我不知道如何调整我的对象模型来修复它。这是我的东西...

1 - 我正在访问的 API/URL: https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&key=xxxxxx

2 - API/URL 返回的内容(来自我的 LogCat)

D/OkHttp: {
D/OkHttp:    "dstOffset" : 0,
D/OkHttp:    "rawOffset" : -28800,
D/OkHttp:    "status" : "OK",
D/OkHttp:    "timeZoneId" : "America/Los_Angeles",
D/OkHttp:    "timeZoneName" : "Pacific Standard Time"
D/OkHttp: }

3 - Logcat 显示对象为空

Log.e(" main ", " apt " + response.body().getResults());
E/ main:  apt null

4 - 调用例程

ApiInterface apiService = ApiClient.getClient(1).create(ApiInterface.class);
Call<ApiTimes> call = apiService.getTime(location,epoch,GOOG_TZ_KEY);
call.enqueue(new Callback<ApiTimes>() {
    @Override
    public void onResponse(Call<ApiTimes> call, Response<ApiTimes> response) {
        int statusCode = response.code();
        Log.e(" main ", " apt " + response.body().getResults());
    }
    @Override
    public void onFailure(Call<ApiTimes> call, Throwable t) {
        Log.e(" getFS ", t.toString());
    }
    });

5 - 对象。

public class ApiTime {

    @SerializedName("apitime")
    private ApiTime apitime;

    @SerializedName("dstOffset")
    @Expose
    private Long dstOffset;
    @SerializedName("rawOffset")
    @Expose
    private Long rawOffset;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("timeZoneId")
    @Expose
    private String timeZoneId;
    @SerializedName("timeZoneName")
    @Expose
    private String timeZoneName;

    public ApiTime(Long dstOffset, Long rawOffset, String status, String timeZoneId, String timeZoneName) {
        this.dstOffset = dstOffset  ;
        this.rawOffset =  rawOffset ;
        this.status = status  ;
        this.timeZoneId = timeZoneId  ;
        this.timeZoneName = timeZoneName  ;
    }

    public ApiTime getResults() {
        return apitime;
    }

    public void setResults(ApiTime apitimes) {
        this.apitime = apitime;
    }

    public Long getDstOffset() {
        return dstOffset;
    }
    public void setDstOffset(Long dstOffset) {
        this.dstOffset = dstOffset;
    }
    public Long getRawOffset() {
        return rawOffset;
    }
    public void setRawOffset(Long rawOffset) {
        this.rawOffset = rawOffset;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getTimeZoneId() {
        return timeZoneId;
    }
    public void setTimeZoneId(String timeZoneId) {
        this.timeZoneId = timeZoneId;
    }
    public String getTimeZoneName() {
        return timeZoneName;
    }
    public void setTimeZoneName(String timeZoneName) {
        this.timeZoneName = timeZoneName;
    }

    @Override
    public String toString() {
        return "ApiTime{" +
                "dstOffset=" + dstOffset +
                ", rawOffset=" + rawOffset +
                ", status='" + status + '\'' +
                ", timeZoneId='" + timeZoneId + '\'' +
                ", timeZoneName='" + timeZoneName + '\'' +
                '}';
    }
}

6 - 这是两个调用例程:

public class ApiClient {
    public static final String URL0 = blah blah
    public static final String URL1 = "https://maps.googleapis.com/maps/api/timezone/";
    private static Retrofit retrofit = null;
    public static  String BASE_URL = "www.google.com";
    public static Retrofit getClient(int url) {

        BASE_URL = URL0;
        if (url == 1){
            BASE_URL = URL1;
        }
        //The following block of code generates OkHttpClient loggging to LogCat.
        //Only use if debugging.
        OkHttpClient.Builder client = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client.addInterceptor(loggingInterceptor);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client.build())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        //Retrofit retrofit = new Retrofit.Builder()
        //        .baseUrl(BASE_URL)
        //        .addConverterFactory(GsonConverterFactory.create())
        //        .build();

        return retrofit;
    }
}

public interface ApiInterface {
    @GET("json")
    Call<ApiTimes> getTime(@Query("location") String location, @Query("timestamp") String timestamp, @Query("key") String key);
}

【问题讨论】:

    标签: java android json rest api


    【解决方案1】:

    看来您应该使用ApiTime 而不是ApiTimes

    我在日志中的 json 中没有看到 @SerializedName("apitimes")

    public interface ApiInterface {
        @GET("json")
        Call<ApiTime> getTime(@Query("location") String location, @Query("timestamp") String timestamp, @Query("key") String key);
    }
    

    -

    ApiInterface apiService = ApiClient.getClient(1).create(ApiInterface.class);
    Call<ApiTime> call = apiService.getTime(location, epoch, GOOG_TZ_KEY);
    call.enqueue(new Callback<ApiTime>() {
        @Override
        public void onResponse(Call<ApiTime> call, Response<ApiTime> response) {
            int statusCode = response.code();
            Log.e("main", "apt " + response.body());
        }
    
        @Override
        public void onFailure(Call<ApiTime> call, Throwable t) {
            Log.e("getFS ", t.toString());
        }
    });
    

    -

    public class ApiTime {
    
        @SerializedName("dstOffset")
        private Long dstOffset;
    
        @SerializedName("rawOffset")
        private Long rawOffset;
    
        @SerializedName("status")
        private String status;
    
        @SerializedName("timeZoneId")
        private String timeZoneId;
    
        @SerializedName("timeZoneName")
        private String timeZoneName;
    
        // getters here
    }
    

    【讨论】:

    • 谢谢 - 请参阅上面对原始帖子的编辑。我删除了对 ApiTimes 的所有引用并重新编码了 apitime,如下所示。我在 ApiTime 中仍然为空。
    • 见第二个代码 sn -p response.body().getResults() -> response.body() getResults() 是多余的
    • 你的意思是response.code();然后 response.body().getResults());?这会导致 ApiTime 为空吗?
    • response.body() 就是这样。 getResults() 是多余的
    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2015-01-11
    • 2021-03-04
    • 2023-02-26
    • 1970-01-01
    相关资源
    最近更新 更多