使用Retrofit解析JSON - 模型架构

时间:2016-10-07 07:24:39

标签: android json nullpointerexception gson retrofit

错误是:

  

方法抛出'java.lang.NullPointerException'异常。无法评估pl.netizens.eonbeacon.storage.EonBeaconModel.toString()

我尝试通过改造来解析JSON。我从服务器获得了JSON,如下所示:

{
  "data": {
    "list": [
      {
        "id": 58,
        "beacon_id": "58",
        "bridge_id": null,
        "mesh_id": null,
        "name": "eon_3c",
        "location": "abcdlk",
        "mac_address": "3c:cf:82:8a:e7:fe",
        "firmware": "2.1",
        "pcb_revision": "2.0",
        "battery_level": "99",
        "password": "0000",
        "ib": {
          "turn_on": "1",
          "major": "33",
          "minor": "344",
          "tx_power": "7",
          "interval": "400",
          "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
          "secure_uuid": "0"
        },
        "euid": {
          "turn_on": "0",
          "namespace": null,
          "instance": null,
          "tx_power": null,
          "interval": null
        },
        "eeid": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null
        },
        "eurl": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null,
          "url": null
        },
        "etlm": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null
        },
        "assigned_bridge_id": null
      },
      {
        "id": 56,
        "beacon_id": "56",
        "bridge_id": null,
        "mesh_id": null,
        "name": "Eon_e6",
        "location": "test",
        "mac_address": "e6:f5:8a:94:b8:c7",
        "firmware": "2.1",
        "pcb_revision": "2.0",
        "battery_level": null,
        "password": "0000",
        "ib": {
          "turn_on": "1",
          "major": "2591",
          "minor": "0",
          "tx_power": "10",
          "interval": "2593",
          "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
          "secure_uuid": "0"
        },
        "euid": {
          "turn_on": "0",
          "namespace": null,
          "instance": null,
          "tx_power": null,
          "interval": null
        },
        "eeid": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null
        },
        "eurl": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null,
          "url": null
        },
        "etlm": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null
        },
        "assigned_bridge_id": null
      },
      {
        "id": 57,
        "beacon_id": "57",
        "bridge_id": "13",
        "mesh_id": "278",
        "name": "Marta B",
        "location": "lk",
        "mac_address": "e1:4b:64:c3:80:e2",
        "firmware": "2.1",
        "pcb_revision": "2.0",
        "battery_level": "97",
        "password": "0000",
        "ib": {
          "turn_on": "1",
          "major": "325",
          "minor": "10",
          "tx_power": "8",
          "interval": "610",
          "uuid": "dae19960-18ea-11e5-8ff0-0002a5d5c521",
          "secure_uuid": "0"
        },
        "euid": {
          "turn_on": "0",
          "namespace": null,
          "instance": null,
          "tx_power": null,
          "interval": null
        },
        "eeid": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null
        },
        "eurl": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null,
          "url": null
        },
        "etlm": {
          "turn_on": "0",
          "tx_power": null,
          "interval": null
        },
        "assigned_bridge_id": null
      }
    ]
  },
  "ver": 1,
  "time": 1475671202,
  "status": 1,
  "status_msg": ""
}

我的根响应类如下所示:

public class BasicResponse<T> {
    @Expose
    public T data;
    @Expose
    public int ver;
    @Expose
    public long time;
    @Expose
    public byte status;
    @Expose
    public String status_msg;
}

到那一刻,一切看起来都不错。现在,我尝试解析数据:

public class BeaconResponse {
    @Expose
    public int id;
    @Expose
    public long beacon_id;
    @Expose
    public long bridge_id;
    @Expose
    public long mesh_id;
    @Expose
    public String name;
    @Expose
    public String location;
    @Expose
    public String mac_address;
    @Expose
    public String firmware;
    @Expose
    public String pcb_revision;
    @Expose
    public byte battery_level;
    @Expose
    public String password;
    @Expose
    @SerializedName("ib")
    public Ib ib;
    @Expose
    @SerializedName("euid")
    public Euid euid;
    @SerializedName("eeid")
    @Expose
    public Eeid eeid;
    @SerializedName("eurl")
    @Expose
    public Eurl eurl;
    @SerializedName("etlm")
    @Expose
    public Etlm etlm;
    @Expose
    public int assigned_bridge_id;

这里问题就出现了。一切都很好解析,除了对象 - euid,eeid,eurl,etlm,ib。当我改变他们的类名时,改造解析除了他们之外的一切都很好 - 他们是空的。当我更改类名以匹配JSON中的键时,我得到了改装错误。我使用了POJO,它创造了与我非常相似的类。知道问题出在哪里? :/

这是我的改装电话:

@Subscribe
    public void getUserBeacons(GetBeaconsListCommand command) {
        Callback<BasicResponse<BeaconsListResponse>> callback = new Callback<BasicResponse<BeaconsListResponse>>() {
            @Override
            public void success(BasicResponse<BeaconsListResponse> basicResponse, Response response) {
                if (ResponseStatus.SUCCESS.statusCode == basicResponse.status)
                    EventBus.post(new BeaconsListFetchedEvent(basicResponse.data));
                else
                    EventBus.post(new NetworkFailureEvent(ResponseStatus.getByStatus(basicResponse.status), basicResponse.status_msg));
            }

            @Override
            public void failure(RetrofitError error) {
            }
        };
        service.beaconsList(callback);
    }

Euid,Eeid Models:

public class EuidModel extends Model {

    @Expose
    public boolean  turn_on;
    @Expose
    public  String namespace;
    @Expose
    public String  instance;
    @Expose
    public  int tx_power;
    @Expose
    public long  interval;

    public boolean isTurn_on() {
        return turn_on;
    }

    public void setTurn_on(boolean turn_on) {
        this.turn_on = turn_on;
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getInstance() {
        return instance;
    }

    public void setInstance(String instance) {
        this.instance = instance;
    }

    public int getTx_power() {
        return tx_power;
    }

    public void setTx_power(int tx_power) {
        this.tx_power = tx_power;
    }

    public long getInterval() {
        return interval;
    }

    public void setInterval(long interval) {
        this.interval = interval;
    }


    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }
public class EeidModel extends Model {

    @Column
    public boolean turn_on;
    @Column
    public int tx_power;
    @Column
    public int interval;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

1 个答案:

答案 0 :(得分:0)

尝试这个模型,我相信它会很好地工作: -

public class model {

    private DataBean data;

    private int ver;
    private int time;
    private int status;
    private String status_msg;

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public int getVer() {
        return ver;
    }

    public void setVer(int ver) {
        this.ver = ver;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public int getStatus() {
        return status;
    }

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

    public String getStatus_msg() {
        return status_msg;
    }

    public void setStatus_msg(String status_msg) {
        this.status_msg = status_msg;
    }

    private static class DataBean {

        private List<ListBean> list;

        public List<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        static class ListBean {
            private int id;
            private String beacon_id;
            private Object bridge_id;
            private Object mesh_id;
            private String name;
            private String location;
            private String mac_address;
            private String firmware;
            private String pcb_revision;
            private String battery_level;
            private String password;

            private IbBean ib;

            private EuidBean euid;

            private EeidBean eeid;

            private EurlBean eurl;

            private EtlmBean etlm;
            private Object assigned_bridge_id;

            public int getId() {
                return id;
            }

            public void setId(int id) {
                this.id = id;
            }

            public String getBeacon_id() {
                return beacon_id;
            }

            public void setBeacon_id(String beacon_id) {
                this.beacon_id = beacon_id;
            }

            public Object getBridge_id() {
                return bridge_id;
            }

            public void setBridge_id(Object bridge_id) {
                this.bridge_id = bridge_id;
            }

            public Object getMesh_id() {
                return mesh_id;
            }

            public void setMesh_id(Object mesh_id) {
                this.mesh_id = mesh_id;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getLocation() {
                return location;
            }

            public void setLocation(String location) {
                this.location = location;
            }

            public String getMac_address() {
                return mac_address;
            }

            public void setMac_address(String mac_address) {
                this.mac_address = mac_address;
            }

            public String getFirmware() {
                return firmware;
            }

            public void setFirmware(String firmware) {
                this.firmware = firmware;
            }

            public String getPcb_revision() {
                return pcb_revision;
            }

            public void setPcb_revision(String pcb_revision) {
                this.pcb_revision = pcb_revision;
            }

            public String getBattery_level() {
                return battery_level;
            }

            public void setBattery_level(String battery_level) {
                this.battery_level = battery_level;
            }

            public String getPassword() {
                return password;
            }

            public void setPassword(String password) {
                this.password = password;
            }

            public IbBean getIb() {
                return ib;
            }

            public void setIb(IbBean ib) {
                this.ib = ib;
            }

            public EuidBean getEuid() {
                return euid;
            }

            public void setEuid(EuidBean euid) {
                this.euid = euid;
            }

            public EeidBean getEeid() {
                return eeid;
            }

            public void setEeid(EeidBean eeid) {
                this.eeid = eeid;
            }

            public EurlBean getEurl() {
                return eurl;
            }

            public void setEurl(EurlBean eurl) {
                this.eurl = eurl;
            }

            public EtlmBean getEtlm() {
                return etlm;
            }

            public void setEtlm(EtlmBean etlm) {
                this.etlm = etlm;
            }

            public Object getAssigned_bridge_id() {
                return assigned_bridge_id;
            }

            public void setAssigned_bridge_id(Object assigned_bridge_id) {
                this.assigned_bridge_id = assigned_bridge_id;
            }

            static class IbBean {
                private String turn_on;
                private String major;
                private String minor;
                private String tx_power;
                private String interval;
                private String uuid;
                private String secure_uuid;

                public String getTurn_on() {
                    return turn_on;
                }

                public void setTurn_on(String turn_on) {
                    this.turn_on = turn_on;
                }

                public String getMajor() {
                    return major;
                }

                public void setMajor(String major) {
                    this.major = major;
                }

                public String getMinor() {
                    return minor;
                }

                public void setMinor(String minor) {
                    this.minor = minor;
                }

                public String getTx_power() {
                    return tx_power;
                }

                public void setTx_power(String tx_power) {
                    this.tx_power = tx_power;
                }

                public String getInterval() {
                    return interval;
                }

                public void setInterval(String interval) {
                    this.interval = interval;
                }

                public String getUuid() {
                    return uuid;
                }

                public void setUuid(String uuid) {
                    this.uuid = uuid;
                }

                public String getSecure_uuid() {
                    return secure_uuid;
                }

                public void setSecure_uuid(String secure_uuid) {
                    this.secure_uuid = secure_uuid;
                }
            }

            static class EuidBean {
                private String turn_on;
                private Object namespace;
                private Object instance;
                private Object tx_power;
                private Object interval;

                public String getTurn_on() {
                    return turn_on;
                }

                public void setTurn_on(String turn_on) {
                    this.turn_on = turn_on;
                }

                public Object getNamespace() {
                    return namespace;
                }

                public void setNamespace(Object namespace) {
                    this.namespace = namespace;
                }

                public Object getInstance() {
                    return instance;
                }

                public void setInstance(Object instance) {
                    this.instance = instance;
                }

                public Object getTx_power() {
                    return tx_power;
                }

                public void setTx_power(Object tx_power) {
                    this.tx_power = tx_power;
                }

                public Object getInterval() {
                    return interval;
                }

                public void setInterval(Object interval) {
                    this.interval = interval;
                }
            }

            static class EeidBean {
                private String turn_on;
                private Object tx_power;
                private Object interval;

                public String getTurn_on() {
                    return turn_on;
                }

                public void setTurn_on(String turn_on) {
                    this.turn_on = turn_on;
                }

                public Object getTx_power() {
                    return tx_power;
                }

                public void setTx_power(Object tx_power) {
                    this.tx_power = tx_power;
                }

                public Object getInterval() {
                    return interval;
                }

                public void setInterval(Object interval) {
                    this.interval = interval;
                }
            }

            static class EurlBean {
                private String turn_on;
                private Object tx_power;
                private Object interval;
                private Object url;

                public String getTurn_on() {
                    return turn_on;
                }

                public void setTurn_on(String turn_on) {
                    this.turn_on = turn_on;
                }

                public Object getTx_power() {
                    return tx_power;
                }

                public void setTx_power(Object tx_power) {
                    this.tx_power = tx_power;
                }

                public Object getInterval() {
                    return interval;
                }

                public void setInterval(Object interval) {
                    this.interval = interval;
                }

                public Object getUrl() {
                    return url;
                }

                public void setUrl(Object url) {
                    this.url = url;
                }
            }

            static class EtlmBean {
                private String turn_on;
                private Object tx_power;
                private Object interval;

                public String getTurn_on() {
                    return turn_on;
                }

                public void setTurn_on(String turn_on) {
                    this.turn_on = turn_on;
                }

                public Object getTx_power() {
                    return tx_power;
                }

                public void setTx_power(Object tx_power) {
                    this.tx_power = tx_power;
                }

                public Object getInterval() {
                    return interval;
                }

                public void setInterval(Object interval) {
                    this.interval = interval;
                }
            }
        }
    }
}