【发布时间】: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 类,当我访问实例的versionName 或version 元素时,我得到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