改造返回空值期望一个属性
Retrofit returning null values expect one attribute
这是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.I 之外的每个值我都得到一个 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;
}
}
响应点:
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 列路径 BEGIN_OBJECT $.Result
如果更改响应则为空值:
您的 Java class $id 和 json $id 相同,以防其他不同。更改 Java class 中的变量名称与 Json 中的对象相同,如下所示。
更改如下
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;
....
}
将您的 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;
另外,你的外层 class 应该是这样的,
public class Client{
@SerializedName("$id")
private String $id;
.
.
@SerializedName("ClientApplicationSettings")
private List<ClientApplicationSettings> clientApplicationSettings
}
并且您得到的 ClientApplicationSettings 值为空,因为如果您仔细观察它期望 ARRAY_OBJECT 但您提供了 STRING_OBJECT.
此外,请记住,始终建议在序列化和反序列化时使用@SerializedName 注释。没有该注释将适用于调试版本,但不适用于发布版本(发布版本已缩小,此处未写完整解释,但如果您好奇,请执行 google 'minification and obfuscation' 处理)。
这部分
public void onResponse(Call<LicenseResponse> call, Response<LicenseResponse> response) {
Log.e("MY gson.JSON: ", "Response Data " + response.body());
}
表示您正在将结果解析为 LicenseResponse
对象。
此 class 只有您的回复中的 $id
,因此它只需要那个。您需要在此处将此 LicenseResponse
更改为 ClientApplicationSettings
,可能在其他地方也可能如此
这是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.I 之外的每个值我都得到一个 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;
}
}
响应点:
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 列路径 BEGIN_OBJECT $.Result
如果更改响应则为空值:
您的 Java class $id 和 json $id 相同,以防其他不同。更改 Java class 中的变量名称与 Json 中的对象相同,如下所示。
更改如下
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;
....
}
将您的 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;
另外,你的外层 class 应该是这样的,
public class Client{
@SerializedName("$id")
private String $id;
.
.
@SerializedName("ClientApplicationSettings")
private List<ClientApplicationSettings> clientApplicationSettings
}
并且您得到的 ClientApplicationSettings 值为空,因为如果您仔细观察它期望 ARRAY_OBJECT 但您提供了 STRING_OBJECT.
此外,请记住,始终建议在序列化和反序列化时使用@SerializedName 注释。没有该注释将适用于调试版本,但不适用于发布版本(发布版本已缩小,此处未写完整解释,但如果您好奇,请执行 google 'minification and obfuscation' 处理)。
这部分
public void onResponse(Call<LicenseResponse> call, Response<LicenseResponse> response) {
Log.e("MY gson.JSON: ", "Response Data " + response.body());
}
表示您正在将结果解析为 LicenseResponse
对象。
此 class 只有您的回复中的 $id
,因此它只需要那个。您需要在此处将此 LicenseResponse
更改为 ClientApplicationSettings
,可能在其他地方也可能如此