【问题标题】:Retrofit Http gives api call successful but returns model elements as nullRetrofit Http 使 api 调用成功,但将模型元素返回为 null
【发布时间】:2017-10-15 14:54:14
【问题描述】:

我正在使用带有 OkHttp 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;
    }

}

我的改造界面:

public interface APIService {

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

}

【问题讨论】:

  • 可能映射失败,请发布您的原始响应和版本类
  • 另外,把你的改造界面放上去
  • @Roy 请检查我已经用版本类和接口更新了我的问题
  • @LuizFernandoSalvaterra 我已经用版本类和接口更新了我的问题
  • 好的,看起来像一个解析器错误,把你所有的 api URL 放在这里,这样我就可以做一些测试了。

标签: android retrofit retrofit2 gson


【解决方案1】:

尝试像这样将 Scalar Converter 添加到您的 Retrofit 实例中:

.addConverterFactory(ScalarsConverterFactory.create())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多