将List发送到Kvm Serializable类

时间:2013-07-29 14:53:34

标签: java android list serialization ksoap2

如何在android上的Ksoap2中实现KvmSerializable的类中发送java.util.List列表?运行应用程序时,我收到以下错误:

java.lang.RuntimeException:无法序列化

我的代码是:

SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.dotNet = false;
env.xsd = SoapSerializationEnvelope.XSD;
env.enc = SoapSerializationEnvelope.ENC;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

Car car= new Car();
byte[] bytes = {1, 2, 3, 4, 5, 6, 7, 8, 9};
car.setBytes(bytes);
List<Door> list = new ArrayList<Door>();
car.setList(list);
PropertyInfo pi = new PropertyInfo();
pi.setName("car");
pi.setValue(car);
pi.setType(car.getClass());
request.addProperty(pi);
env.setOutputSoapObject(request);
env.addMapping(NAMESPACE, "Car", byte[].class, new MarshalBase64());

汽车班:

public class Car implements KvmSerializable{

    private byte[] bytes;
    private List<Door> list;

    public void setBytes(byte[] bytes) {
        this.bytes= bytes;
    }

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

    @Override
    public Object getProperty(int arg0) {
        switch(arg0){
            case 0:
                return bytes;
            case 1:
                return list;

        }

         return null;
    }

    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 2
    }

    @Override
    public void getPropertyInfo(int ind, Hashtable ht, PropertyInfo info) {
        switch(ind){
                case 0:
                    info.type = MarshalBase64.BYTE_ARRAY_CLASS;
                    info.name = "bytes";
                    break;
                 case 1
                    info.type = List.class;
                    info.name = "list";
                    break;

                default:break;
        }

    }

    @Override
    public void setProperty(int ind, Object val) {
        switch(ind){
                case 0:
                    bytes = (byte[])val;
                    break;
                case 1:
                    list= (List<Door>)val;
                    break;

        }

    }

}

Door类实现了KvmSerializable,任何人都可以帮助我吗?

感谢

1 个答案:

答案 0 :(得分:1)

有几种方法可以在ksoap2中使类可序列化:

  1. 为此课程创建马歇尔对象,为其创建addMapping
  2. 实施界面KvmSerializable
  3. 默认情况下,
  4. SoapObjectVector可序列化。
  5. List与任何此情况都不匹配,因此您收到错误消息。 您可以为类List创建自定义marshall对象,也可以创建实现List接口和KvmSerializable的自定义类。或者您可以使用Vector(或者甚至可以通过调用addPropertyaddSoapObject将您的对象转换为SoapObject。)