sendding List into a KvmSerializable class

空扰寡人 提交于 2019-12-11 14:57:11

问题


how I can to send a List of java.util.List inside a class that implements KvmSerializable in Ksoap2 on android?. when running the application I get the following error:

java.lang.RuntimeException: Can not serialize

my code is this:

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());

the Car class:

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;

        }

    }

}

the Door class implements KvmSerializable, anyone can help me?

thanks


回答1:


There are several ways to make class serializable in ksoap2:

  1. Create marshall object for this class and addMapping for it
  2. Implement interface KvmSerializable
  3. SoapObject and Vector serializable by default.

List does not match any of this condition, so you get an error. You can create custom marshall object for class List or create custom class which would implement List interface and KvmSerializable. Or you can use Vector (or may be even convert your object to SoapObject by calling addProperty and addSoapObject).



来源:https://stackoverflow.com/questions/17927229/sendding-list-into-a-kvmserializable-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!