Retrofit Http给api调用成功,但返回模型元素为null

时间:2017-05-16 13:29:57

标签: android retrofit retrofit2 gson

我正在使用OkTttp 3.8.0的Retrofit版本2.3.0。当我使用GET请求点击我的api时,它返回200作为结果,但我无法访问Version模型中的元素。

APIService apiService = APIClient.getAPIService();

Call<Version> call = apiService.getVersionByVersionCode(BuildConfig.VERSION_CODE);
        call.enqueue(new Callback<Version>() {
            @Override
            public void onResponse(Call<Version> call, Response<Version> response) {
                Version result = response.body();
                Log.e("TAG", "OnResponse " + result.toString());
            }

            @Override
            public void onFailure(Call<Version> call, Throwable t) {
                Log.e("TAG", "Failure ");
            }
        });

这是我的Version课程,当我访问实例的versionNameversion元素时,我得到null

package com.actovoice.model;

import com.google.gson.annotations.SerializedName;

public class Version {

    @SerializedName("upgrade") int upgrade;
    @SerializedName("versionName") String versionName;
    @SerializedName("version") String version;

    public int getUpgrade() {
        return upgrade;
    }

    public void setUpgrade(int upgrade) {
        this.upgrade = upgrade;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

}

My Retrofit界面:

public interface APIService {

    @GET("apps/appType/ANDROID/version/{versionCode}")
    Call<Version> getVersionByVersionCode(@Path("versionCode") int versionCode);

}

1 个答案:

答案 0 :(得分:0)

尝试将Scalar Converter添加到您的Retrofit实例中,如下所示:

.addConverterFactory(ScalarsConverterFactory.create())