Home > Software design >  Retrofit returning null values expect one attribute
Retrofit returning null values expect one attribute

Time:12-24

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:

    private String $id;
    private String clientAppSettingsId;
    private String licenseKey;
    private String settingsType;
    private String moduleName;
    private String addonName;
    private String className;
    private String allowAccess;
    private String settingValue;
    private String displayOrder;
    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<LicenseSettings> call = APIClient.getInstance().getMyApi().getSettings(settingParameter);
        call.enqueue(new Callback<LicenseSettings>() {
            @Override
            public void onResponse(Call<LicenseSettings> call, Response<LicenseSettings> response) {
                //System.out.println("onResponse");
                //System.out.println(response.body().toString());
                Log.e("MY gson.JSON:  ", "Response Data "   response.body());


            }

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

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

            }
        });

Response am getting is

CodePudding user response:

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

CodePudding user response:

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).

  • Related