Siva
Siva

Reputation: 1861

Retrofit returning null values expect one attribute

Here is JSON response sample

{
"$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
},**

I'm trying to use retrofit to parse the JSON and create the interface. The problem that I have is that I get a null for every value except $id.I need only the ClientApplicationSettings Array value

I have a ClientApplicationSettings pojo that looks like this:

       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;
    }
}

Response 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;
    }
}

Here is my retrofit

SettingParameter settingParameter = new SettingParameter(); settingParameter.setClientCode("101"); settingParameter.setApplicationCode("WMSPC"); //settingParameter.setClientCode(strClientCode); settingParameter.setDeviceId(androidId); //settingParameter.setDeviceId("868785042115222"); settingParameter.setDeviceName(deviceName); 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();

    }
});

And am getting the below error now: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 56 path $.Result

null values if changes the response: enter image description here

Upvotes: 1

Views: 655

Answers (3)

Ivo
Ivo

Reputation: 23164

This part

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

}

indicates that you are parsing the result as a LicenseResponse object.

This class only has the $id from the things in your response so it only takes that. You need to change this LicenseResponse to ClientApplicationSettings here, and probably at other places as well

Upvotes: 0

Shubham
Shubham

Reputation: 131

Change your ClientApplicationSettings POJO to

@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;

Also, your outer class should be something like this,

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

And you were getting a null value for ClientApplicationSettings because if you carefully observe it was expecting ARRAY_OBJECT but you provided STRING_OBJECT.

Also, keep in mind it is always advised to use @SerializedName annotations while serializing and deserializing. Not having that annotation will work on debug builds but not on Release builds(release builds are minified, not writing a complete explanation here but do google 'minification and obfuscation' process if you are curious).

Upvotes: 1

Daniyal Ahmed Khan
Daniyal Ahmed Khan

Reputation: 88

Your Java class $id and json $id are same in case rest are not. Change variable name in Java class same as in Json Object like below.

Change as below

private String status;
to 
private String Status;

UPDATED Is your ClientApplicationSettings pojo like this? Because its a list

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; 
.... 
}

Upvotes: 1

Related Questions