【问题标题】:Retrofit returning null values expect one attribute改造返回空值期望一个属性
【发布时间】:2022-01-24 14:21:27
【问题描述】:

这是 JSON 响应示例

{
"$id": "1",
"ErrorCode": 0,
"ErrorMessage": null,
"Result": {
"$id": "2",
"LicenseId": 26,
"ClientId": 3,
"ClientCode": "101",
"ClientName": "Silver Creek Software1",
"AppId": 1012,
"ApplicationName": "WMS Picking Client",
"LicenseKey": "5120-4065-1531-B375",
"LicenseType": "D",
"TimeRegistered": null,
"LicenseCount": 1,
"Status": true,
"ClientApplicationSettings": [ {
"$id": "3",
**"ClientAppSettingsId": 87,
"LicenseKey": "5120-4065-1531-B375",
"SettingsType": "S",
"ModuleName": null,
"AddonName": "Namespace",
"ClassName": null,
"AllowAccess": false,
"SettingValue": "http://silvercreek.com/WMSPickingClient",
"DisplayOrder": 1,
"Status": true
},**

我正在尝试使用改造来解析 JSON 并创建接口。我遇到的问题是除了 $id 之外的每个值都为 null。我只需要 ClientApplicationSettings 数组值

我有一个如下所示的 ClientApplicationSettings pojo:

       public class ClientApplicationSettings {

    @SerializedName("$id")
    private String $id;
    @SerializedName("ClientAppSettingsId")
    private String clientAppSettingsId;
    @SerializedName("LicenseKey")
    private String licenseKey;
    @SerializedName("SettingsType")
    private String settingsType;
    @SerializedName("ModuleName")
    private String moduleName;
    @SerializedName("AddonName")
    private String addonName;
    @SerializedName("ClassName")
    private String className;
    @SerializedName("AllowAccess")
    private String allowAccess;
    @SerializedName("SettingValue")
    private String settingValue;
    @SerializedName("DisplayOrder")
    private String displayOrder;
    @SerializedName("Status")
    private String status;

    public String get$id() {
        return $id;
    }

    public void set$id(String $id) {
        this.$id = $id;
    }

    public String getClientAppSettingsId() {
        return clientAppSettingsId;
    }

    public void setClientAppSettingsId(String clientAppSettingsId) {
        this.clientAppSettingsId = clientAppSettingsId;
    }

    public String getLicenseKey() {
        return licenseKey;
    }

    public void setLicenseKey(String licenseKey) {
        this.licenseKey = licenseKey;
    }

    public String getSettingsType() {
        return settingsType;
    }

    public void setSettingsType(String settingsType) {
        this.settingsType = settingsType;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getAddonName() {
        return addonName;
    }

    public void setAddonName(String addonName) {
        this.addonName = addonName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getAllowAccess() {
        return allowAccess;
    }

    public void setAllowAccess(String allowAccess) {
        this.allowAccess = allowAccess;
    }

    public String getSettingValue() {
        return settingValue;
    }

    public void setSettingValue(String settingValue) {
        this.settingValue = settingValue;
    }

    public String getDisplayOrder() {
        return displayOrder;
    }

    public void setDisplayOrder(String displayOrder) {
        this.displayOrder = displayOrder;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

回复pojo:

public class LicenseResponse {
    private String $id;
    private String ErrorCode;
    private String ErrorMessage;
    private List<Result> Result;

    public String get$id() {
        return $id;
    }

    public void set$id(String $id) {
        this.$id = $id;
    }

    public String getErrorCode() {
        return ErrorCode;
    }

    public void setErrorCode(String errorCode) {
        ErrorCode = errorCode;
    }

    public String getErrorMessage() {
        return ErrorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        ErrorMessage = errorMessage;
    }


     public List<Result> getResult() {
        return Result;
    }

    public void setResult(List<Result> result) {
        Result = result;
    }
}

ClientSettings pojo:

public class ClientApplicationSettings {

    @SerializedName("$id")
    private String $id;
    @SerializedName("ClientAppSettingsId")
    private String clientAppSettingsId;
    @SerializedName("LicenseKey")
    private String licenseKey;
    @SerializedName("SettingsType")
    private String settingsType;
    @SerializedName("ModuleName")
    private String moduleName;
    @SerializedName("AddonName")
    private String addonName;
    @SerializedName("ClassName")
    private String className;
    @SerializedName("AllowAccess")
    private String allowAccess;
    @SerializedName("SettingValue")
    private String settingValue;
    @SerializedName("DisplayOrder")
    private String displayOrder;
    @SerializedName("Status")
    private String status;

    public String get$id() {
        return $id;
    }

    public void set$id(String $id) {
        this.$id = $id;
    }

    public String getClientAppSettingsId() {
        return clientAppSettingsId;
    }

    public void setClientAppSettingsId(String clientAppSettingsId) {
        this.clientAppSettingsId = clientAppSettingsId;
    }

    public String getLicenseKey() {
        return licenseKey;
    }

    public void setLicenseKey(String licenseKey) {
        this.licenseKey = licenseKey;
    }

    public String getSettingsType() {
        return settingsType;
    }

    public void setSettingsType(String settingsType) {
        this.settingsType = settingsType;
    }

    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getAddonName() {
        return addonName;
    }

    public void setAddonName(String addonName) {
        this.addonName = addonName;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getAllowAccess() {
        return allowAccess;
    }

    public void setAllowAccess(String allowAccess) {
        this.allowAccess = allowAccess;
    }

    public String getSettingValue() {
        return settingValue;
    }

    public void setSettingValue(String settingValue) {
        this.settingValue = settingValue;
    }

    public String getDisplayOrder() {
        return displayOrder;
    }

    public void setDisplayOrder(String displayOrder) {
        this.displayOrder = displayOrder;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}

这是我的改造

SettingParameter settingParameter = new SettingParameter(); settingParameter.setClientCode("101"); settingParameter.setApplicationCode("WMSPC"); //settingParameter.setClientCode(strClientCode); settingParameter.setDeviceId(androidId); //settingParameter.setDeviceId("868785042115222"); settingParameter.setDeviceName(设备名); settingParameter.setDeviceType("Android");

Call<LicenseResponse> call = APIClient.getInstance().getMyApi().getSettings(settingParameter);
call.enqueue(new Callback<LicenseResponse>() {
    @Override
    public void onResponse(Call<LicenseResponse> call, Response<LicenseResponse> response) {
        Log.e("MY gson.JSON:  ", "Response Data " + response.body());

    }

    @Override
    public void onFailure(Call<LicenseResponse> call, Throwable t) {
        String errorMsg = t.getMessage();
        if(errorMsg.equalsIgnoreCase("timeout")){
            getSettings();
        }else {

        }
        Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();

    }
});

现在出现以下错误: java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 56 列路径 $.Result 处为 BEGIN_OBJECT

如果更改响应则为空值:

【问题讨论】:

  • 请同时发布您的 LicenseSettings 类。看来问题出在那儿了
  • @Shahnawaz 已更新。请检查。

标签: android api retrofit


【解决方案1】:

你的 Java 类 $id 和 json $id 是相同的,以防其余的不同。更改 Java 类中的变量名称,如下所示。

如下修改

private String status;
to 
private String Status;

更新 你的 ClientApplicationSettings pojo 是这样的吗?因为它是一个列表

public class Example
 { 
"$id": "1", 
"ErrorCode": 0, 
... 
Public List<ClientApplicationSettings> client; }
 And 
Public class ClientApplicationSettings 
{ 
private String $id; 
private String ClientAppSettingsId; 
private String LicenseKey; 
private String SettingsType; 
private String ModuleName; 
.... 
}

【讨论】:

  • 我按照你的评论改了,还是遇到同样的问题
  • 仅在此处从 JAVA 类粘贴变量。
  • 私有字符串 $id;私有字符串 ClientAppSettingsId;私有字符串 LicenseKey;私有字符串设置类型;私有字符串模块名称;私人字符串插件名称;私有字符串类名;私有字符串允许访问;私有字符串设置值;私有字符串 DisplayOrder;私有字符串状态;
  • 尝试改变你唯一的变量。 @SerializedName("ClientAppSettingsId")@Expose 私有字符串 ClientAppSettingsId; @SerializedName("LicenseKey") @Expose private String LicenseKey;
  • 试过上面那个。同样的反应
【解决方案2】:

将您的 ClientApplicationSettings POJO 更改为

@SerializedName("$id")
private String $id;
@SerializedName("ClientAppSettingsId")
private String clientAppSettingsId;
@SerializedName("LicenseKey")
private String licenseKey;
@SerializedName("SettingsType")
private String settingsType;
@SerializedName("ModuleName")
private String moduleName;
@SerializedName("AddonName")
private String addonName;
@SerializedName("ClassName")
private String className;
@SerializedName("AllowAccess")
private String allowAccess;
@SerializedName("SettingValue")
private String settingValue;
@SerializedName("DisplayOrder")
private String displayOrder;
@SerializedName("Status")
private String status;

另外,你的外部类应该是这样的,

public class Client{
@SerializedName("$id")
private String $id;
.
.
@SerializedName("ClientApplicationSettings")
private List<ClientApplicationSettings> clientApplicationSettings
}

您得到了 ClientApplicationSettings 的空值,因为如果您仔细观察它期待 ARRAY_OBJECT 但您提供了 STRING_OBJECT。

另外,请记住,始终建议在序列化和反序列化时使用 @SerializedName 注释。没有该注释将适用于调试版本,但不适用于发布版本(发布版本被缩小,这里不写完整的解释,但如果你好奇,请执行谷歌“缩小和混淆”过程)。

【讨论】:

  • 我已根据建议更新了模型,现在出现了不同的错误。我可以理解错误,但我不知道要更改什么。
【解决方案3】:

这部分

public void onResponse(Call<LicenseResponse> call, Response<LicenseResponse> response) {
    Log.e("MY gson.JSON:  ", "Response Data " + response.body());

}

表示您将结果解析为LicenseResponse 对象。

这个类只有 $id 来自你的回复中的东西,所以它只需要那个。您需要在此处将 LicenseResponse 更改为 ClientApplicationSettings,可能在其他地方也可以

【讨论】:

  • 我已根据您的建议进行了更改,但我得到了除 id 之外的空值。我已经更新了我的问题中的错误图片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 2020-05-06
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多