Skip to content Skip to sidebar Skip to footer

Retrofit, Parsing Json Null Exception At Response

I'm using Retrofit in my app. I've got JSON response which looks like this: { 'data': { 'list': [ { 'id': 58, 'beacon_id': '58', 'bridge_id': nu

Solution 1:

Perhaps you should create your response class like this:

@SerializedName("list")
@Expose
private List<Data> list = new ArrayList<Data>();

Try creating your Response class through this tool:

http://www.jsonschema2pojo.org/

Solution 2:

The issue is that your Java classes do not mimic your JSON schema. You Java classes need to match the JSON schema exactly, or GSON will not be able to deserialize the data. This should better represent your schema:

publicclassStandardResponse<T> {

    @SerializedName("data")public T data;
    @SerializedName("ver")public Integer version;
    @SerializedName("time")publicLong time;
    @SerializedName("status")public Integer status;
    @SerializedName("status_msg")public String statusMessage;

}

Then you can implement a ListData class that contains a List:

publicclassListData<T> {

    @SerializedName("list")public List<T> list;

}

Then on to your Beacon class:

publicclassBeacon{

    @SerializedName("id")public Integer id;
    @SerializedName("beacon_id")public String beaconID;
    @SerializedName("bridge_id")public String bridgeID;
    @SerializedName("mesh_id")public String meshID;
    @SerializedName("name")public String name;
    @SerializedName("location")public String location;
    @SerializedName("mac_address")public String macAddress;
    @SerializedName("firmware")public String firmware;
    @SerializedName("pcb_revision")public String pcbRevision;
    @SerializedName("battery_level")public String batteryLevel;
    @SerializedName("password")public String password;
    @SerializedName("ib")public Ib ib;
    @SerializedName("euid")public Euid euid;
    @SerializedName("eeid")public Eeid eeid;
    @SerializedName("eurl")public Eurl eurl;
    @SerializedName("etlm")public Etlm etlm;
    @SerializedName("assigned_bridge_id")public String assignedBridgeID;

}

I will skip your Ib, Euid, Edid, Eurl and Etlm classes, as they should not be too difficult to implement based on the classes above.


With these classes, your Retrofit Callback should look like this:

@GET("/api/mobile/{language}/{apiVersion}/beacons/list")voidgetBeacons(Callback<StandardResponse<ListData<Beacon>> callback);

And finally, you can obtain your list of Beacon objects from the callback like so:

service.getBeacons(newCallback<StandardResponse<ListData<Beacon>>> {

    @Overridepublicvoidsuccess(StandardResponse<ListData<Beacon>> standard, Response response) {
        Log.d(TAG, "Status: " + standard.status + "; with message: " + standard.statusMessage);
        List<Beacon> beacons = standard.data.list;

        for(Beaconbeacon: beacons) {
            Log.d(TAG, "Beacon ID: " + beacon.id);
        }
    }

    @Overridepublicvoidfailure(RetrofitError error) {
        // TODO: implement error handling
    }

});

Based on your sample JSON, the above code should print the following in logcat:

Status:1,with message:Beacon ID:58Beacon ID:56Beacon ID:57

Update

Here are the Ib, Euid, Edid, Eurl and Etlm classes:

publicclassIb{

    @SerializedName("turn_on")public String turnOn;
    @SerializedName("major")public String major;
    @SerializedName("minor")public String minor;
    @SerializedName("tx_power")public String txPower;
    @SerializedName("interval")public String interval;
    @SerializedName("uuid")public String uuid;
    @SerializedName("secure_uuid")public String uuidSecure;

}

publicclassEuid{

    @SerializedName("turn_on")public String turnOn;
    @SerializedName("namespace")public String nameSpace;
    @SerializedName("instance")public String instance;
    @SerializedName("tx_power")public String txPower;
    @SerializedName("interval")public String interval;

}

publicclassEdid{

    @SerializedName("turn_on")public String turnOn;
    @SerializedName("tx_power")public String txPower;
    @SerializedName("interval")public String interval;

}

publicclassEurl{

    @SerializedName("turn_on")public String turnOn;
    @SerializedName("tx_power")public String txPower;
    @SerializedName("interval")public String interval;
    @SerializedName("url")public String url;

}

publicclassEtlm{

    @SerializedName("turn_on")public String turnOn;
    @SerializedName("tx_power")public String txPower;
    @SerializedName("interval")public String interval;

}

Post a Comment for "Retrofit, Parsing Json Null Exception At Response"