我正在尝试将SoapRequest反序列化为复杂对象。这是XML响应:
<ArrayOfECar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://testing.com/">
<eCar>
<Id>1</Id>
<Description>Fiat</Description>
<Drivers>
<eDriver>
<Id>1</Id>
<Description>Lucas</Description>
</eDriver>
<eDriver>
<Id>2</Id>
<Description>John</Description>
</eDriver>
</Drivers>
</eCar>
<eCar>
<Id>2</Id>
<Description>Ford</Description>
<Drivers>
<eDriver>
<Id>1</Id>
<Description>Charly</Description>
</eDriver>
<eDriver>
<Id>2</Id>
<Description>Harry</Description>
</eDriver>
</Drivers>
</eCar>
</ArrayOfECar>
现在,在我的Android项目中,我创建了Object Classes实现KvmSerializable,如下所示:
汽车类:
public class Car implements KvmSerializable {
private int id;
private String description;
private ArrayDriver drivers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayDriver getDrivers() {
return drivers;
}
public void setDrivers(ArrayDriver drivers) {
this.drivers = drivers;
}
@Override
public Object getProperty(int index) {
switch (index) {
case 0:
return getId();
case 1:
return getDescription();
case 2:
return getDrivers();
}
return null;
}
@Override
public int getPropertyCount() {
return 3;
}
@Override
public void setProperty(int index, Object value) {
switch (index) {
case 0:
setId(Integer.parseInt(value.toString()));
break;
case 1:
setDescription(value.toString());
break;
case 2:
setDrivers((ArrayDriver)value);
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.name = "Id";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "Description";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "Drivers";
info.type = ArrayDriver.class;
break;
default:
break;
}
}
}
驱动程序类:
public class Driver implements KvmSerializable {
private int id;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public Object getProperty(int index) {
switch (index) {
case 0:
return getId();
case 1:
return getDescription();
}
return null;
}
@Override
public int getPropertyCount() {
return 2;
}
@Override
public void setProperty(int index, Object value) {
switch (index) {
case 0:
setId(Integer.parseInt(value.toString()));
break;
case 1:
setDescription(value.toString());
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.name = "Id";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "Description";
info.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
}
这样的数组:
ArrayCar类:
public class ArrayCar extends Vector<Car> implements KvmSerializable {
@Override
public Object getProperty(int index) {
return this.get(index);
}
@Override
public int getPropertyCount() {
return this.size();
}
@Override
public void setProperty(int index, Object value) {
this.add((Car)value);
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
info.name = "eCar";
info.type = Car.class;
}
}
ArrayDriver类:
public class ArrayDriver extends Vector<Driver> implements KvmSerializable {
@Override
public Object getProperty(int index) {
return this.get(index);
}
@Override
public int getPropertyCount() {
return this.size();
}
@Override
public void setProperty(int index, Object value) {
this.add((Driver)value);
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
info.name = "eDriver";
info.type = Driver.class;
}
}
现在这里是我用来将XML反序列化为ArrayCar的AsyncTask:
String SOAP_ACTION = "http://testing.com/getCars";
String METHOD_NAME = "getCars";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "eCar", Car.class);
envelope.addMapping(NAMESPACE, "eDriver", Driver.class);
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
try {
transport.call(SOAP_ACTION, envelope);
return (ArrayCar) envelope.getResponse();
} catch (Exception e) {
Log.e("TEST", "KSOAP2", e);
}
return null;
但它没有用,抛出这个例外:
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to com.oseas.wsconsumer.entities.ArrayCar
我知道我可以手动反序列化,通过SoapObject中的每个属性循环等等。但我想知道是否有一种方法可以使它更容易和自动化。
抱歉英语不好,希望你能理解。