我正在尝试解析下一个SOAP响应:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<tt_statemonitorMobileAdResponse xmlns="http://monitoring-gps.com.ua/aspservices/">
<tt_statemonitorMobileAdResult>
<statuscode>4</statuscode>
<uiaction>0</uiaction>
<data>
<anyType xsi:type="TtMobileStateData">
<tt_id>1000002502</tt_id>
<speed>0</speed>
<angle>0</angle>
<ign>0</ign>
<fuel>132</fuel>
<driver_id>-1</driver_id>
<satellites>20</satellites>
<driver />
<name>AA0873KT</name>
<lat>50.4441</lat>
<lon>30.6471</lon>
<date>07-10-2017 16:11:50</date>
<address>32, Гродненская улица, Дарница, Днепровский район, Киев, 02090, Украина</address>
<actual>1</actual>
<is_bs>0</is_bs>
<bsList />
<isMove>false</isMove>
<necessary_to_replace>false</necessary_to_replace>
<movesec>0</movesec>
<stopsec>20075</stopsec>
<devicetype_id>1</devicetype_id>
<devicetype>Автомобильный Трекер</devicetype>
<devicemodel_id>16</devicemodel_id>
<devicemodel>BI 864 TREK v2</devicemodel>
<battery_replacement />
<pointCount>0</pointCount>
<timeconnect>07.10.2017 16:11:24</timeconnect>
</anyType>
<anyType xsi:type="TtMobileStateData">
<tt_id>1000002514</tt_id>
<speed>69</speed>
<angle>147</angle>
<ign>1</ign>
<fuel>316</fuel>
<driver_id>-1</driver_id>
<satellites>20</satellites>
<driver />
<name>AA0872KT</name>
<lat>48.5748</lat>
<lon>23.0335</lon>
<date>07-10-2017 16:12:00</date>
<address>М-06, Свалявский район, Закарпатская область, 89308, Украина</address>
<actual>1</actual>
<is_bs>0</is_bs>
<bsList />
<isMove>true</isMove>
<necessary_to_replace>false</necessary_to_replace>
<movesec>1243</movesec>
<stopsec>0</stopsec>
<devicetype_id>1</devicetype_id>
<devicetype>Автомобильный Трекер</devicetype>
<devicemodel_id>16</devicemodel_id>
<devicemodel>BI 864 TREK v2</devicemodel>
<battery_replacement />
<pointCount>0</pointCount>
<timeconnect>07.10.2017 15:59:33</timeconnect>
</anyType>
<anyType xsi:type="TtMobileStateData">
<tt_id>1000002527</tt_id>
<speed>0</speed>
<angle>0</angle>
<ign>0</ign>
<fuel>172</fuel>
<driver_id>-1</driver_id>
<satellites>0</satellites>
<driver />
<name>AA2049EA</name>
<lat>50.444</lat>
<lon>30.647</lon>
<date>07-10-2017 16:09:56</date>
<address>32, Гродненская улица, Дарница, Днепровский район, Киев, 02090, Украина</address>
<actual>1</actual>
<is_bs>0</is_bs>
<bsList />
<isMove>false</isMove>
<necessary_to_replace>false</necessary_to_replace>
<movesec>0</movesec>
<stopsec>16120</stopsec>
<devicetype_id>1</devicetype_id>
<devicetype>Автомобильный Трекер</devicetype>
<devicemodel_id>16</devicemodel_id>
<devicemodel>BI 864 TREK v2</devicemodel>
<battery_replacement />
<pointCount>0</pointCount>
<timeconnect>07.10.2017 16:09:33</timeconnect>
</anyType>
<anyType xsi:type="TtMobileStateData">
<tt_id>1000003365</tt_id>
<speed>0</speed>
<angle>0</angle>
<ign>0</ign>
<fuel>0</fuel>
<driver_id>-1</driver_id>
<satellites>16</satellites>
<driver />
<name>Hilux 73</name>
<lat>49.5637</lat>
<lon>34.5297</lon>
<date>07-10-2017 16:12:17</date>
<address>Европейская улица, Полтава, Шевченковский район, Полтава, Полтавская область, 36000-36499, Украина</address>
<actual>1</actual>
<is_bs>0</is_bs>
<bsList />
<isMove>false</isMove>
<necessary_to_replace>false</necessary_to_replace>
<movesec>0</movesec>
<stopsec>89852</stopsec>
<devicetype_id>1</devicetype_id>
<devicetype>Автомобильный Трекер</devicetype>
<devicemodel_id>18</devicemodel_id>
<devicemodel>BI 820 TREK</devicemodel>
<battery_replacement />
<pointCount>0</pointCount>
<timeconnect>07.10.2017 14:43:32</timeconnect>
</anyType>
</data>
<length>4</length>
<ttstates>
<tt_id>0</tt_id>
<speed>0</speed>
<angle>0</angle>
<ign>0</ign>
<fuel>0</fuel>
<driver_id>0</driver_id>
<satellites>0</satellites>
<lat>0</lat>
<lon>0</lon>
<actual>0</actual>
<is_bs>0</is_bs>
<isMove>false</isMove>
<necessary_to_replace>false</necessary_to_replace>
<movesec>0</movesec>
<stopsec>0</stopsec>
<devicetype_id>0</devicetype_id>
<devicemodel_id>0</devicemodel_id>
<pointCount>0</pointCount>
</ttstates>
</tt_statemonitorMobileAdResult>
</tt_statemonitorMobileAdResponse>
</soap:Body>
</soap:Envelope>
为此,我正在使用Retrofit2 + Simple XML Converter。
以下是一些Java类。
StatemonitorResponseData.java
@Root(name = "data", strict = false)
public class StatemonitorResponseData {
@ElementList(name = "anyType", required = false, inline = true)
protected List<StatemonitorListModel> statemonitorList;
public List<StatemonitorListModel> getStatemonitorList() {
return statemonitorList;
}
public void setStatemonitorList(List<StatemonitorListModel> statemonitorList) {
this.statemonitorList = statemonitorList;
}
}
StatemonitorListModel.java
@Root(name = "anyType" , strict = false)
public class StatemonitorListModel {
@Element(name = "tt_id")
private int tt_id;
@Element(name = "speed")
private int speed;
@Element(name = "angle")
private int angle;
@Element(name = "ign")
private int ign;
@Element(name = "fuel")
private int fuel;
@Element(name = "driver_id")
private int driver_id;
@Element(name = "satellites")
private int satellites;
@Element(name = "name")
private String name;
@Element(name = "lat")
private double lat;
@Element(name = "lon")
private double lon;
@Element(name = "date")
private String date;
@Element(name = "address")
private String address;
@Element(name = "actual")
private int actual;
@Element(name = "is_bs")
private int is_bs;
@Element(name = "isMove")
private boolean isMove;
@Element(name = "necessary_to_replace")
private boolean necessary_to_replace;
@Element(name = "movesec")
private int movesec;
@Element(name = "stopsec")
private int stopsec;
@Element(name = "devicetype_id")
private int devicetype_id;
@Element(name = "devicetype")
private String devicetype;
@Element(name = "devicemodel_id")
private int devicemodel_id;
@Element(name = "devicemodel")
private String devicemodel;
@Element(name = "pointCount")
private int pointCount;
@Element(name = "timeconnect")
private String timeconnect;
public int getTt_id() {
return tt_id;
}
public void setTt_id(int tt_id) {
this.tt_id = tt_id;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getAngle() {
return angle;
}
public void setAngle(int angle) {
this.angle = angle;
}
public int getIgn() {
return ign;
}
public void setIgn(int ign) {
this.ign = ign;
}
public int getFuel() {
return fuel;
}
public void setFuel(int fuel) {
this.fuel = fuel;
}
public int getDriver_id() {
return driver_id;
}
public void setDriver_id(int driver_id) {
this.driver_id = driver_id;
}
public int getSatellites() {
return satellites;
}
public void setSatellites(int satellites) {
this.satellites = satellites;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getActual() {
return actual;
}
public void setActual(int actual) {
this.actual = actual;
}
public int getIs_bs() {
return is_bs;
}
public void setIs_bs(int is_bs) {
this.is_bs = is_bs;
}
public boolean isMove() {
return isMove;
}
public void setMove(boolean move) {
isMove = move;
}
public boolean isNecessary_to_replace() {
return necessary_to_replace;
}
public void setNecessary_to_replace(boolean necessary_to_replace) {
this.necessary_to_replace = necessary_to_replace;
}
public int getMovesec() {
return movesec;
}
public void setMovesec(int movesec) {
this.movesec = movesec;
}
public int getStopsec() {
return stopsec;
}
public void setStopsec(int stopsec) {
this.stopsec = stopsec;
}
public int getDevicetype_id() {
return devicetype_id;
}
public void setDevicetype_id(int devicetype_id) {
this.devicetype_id = devicetype_id;
}
public String getDevicetype() {
return devicetype;
}
public void setDevicetype(String devicetype) {
this.devicetype = devicetype;
}
public int getDevicemodel_id() {
return devicemodel_id;
}
public void setDevicemodel_id(int devicemodel_id) {
this.devicemodel_id = devicemodel_id;
}
public String getDevicemodel() {
return devicemodel;
}
public void setDevicemodel(String devicemodel) {
this.devicemodel = devicemodel;
}
public int getPointCount() {
return pointCount;
}
public void setPointCount(int pointCount) {
this.pointCount = pointCount;
}
public String getTimeconnect() {
return timeconnect;
}
public void setTimeconnect(String timeconnect) {
this.timeconnect = timeconnect;
}
Retrofit的回复是成功的,但是当我尝试获取列表时,它会向我显示下一个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.monitoring.gpsmayak.api.response.statemonitor.StatemonitorResponseData.getStatemonitorList()' on a null object reference
请告诉我应该在代码中更改哪些内容。 我不确定,但我认为问题是我不知道如何解析 xsi:type =“TtMobileStateData”。
我在这里也发现了类似的问题: Cannot get SOAP envelope body using Retrofit 2 and Simple XML Converter 但我不知道如何在我的案例中实现这个
答案 0 :(得分:1)
问题解决了。 我刚刚改变了
@ElementList(name = "anyType", required = false, inline = true)
到
@ElementList(entry = "anyType", required = false, inline = true)