0

如何在 android 上的 Ksoap2 中实现 KvmSerializable 的类中发送一个字节 [] 数组?运行应用程序时出现以下错误:

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);
PropertyInfo pi = new PropertyInfo();
pi.setName("car");
pi.setValue(car);
pi.setType(car.getClass());
request.addProperty(pi);
env.setOutputSoapObject(request);
env.addMapping(NAMESPACE, "Car", car.getClass());

汽车类:

public class Car implements KvmSerializable{

    private byte[] bytes;

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

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

        }

         return null;
    }

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

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

                default:break;
        }

    }

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

        }

    }

}

任何人都可以帮助我吗?

谢谢

4

1 回答 1

1

您应该使用 marshaller 为 class 添加映射byte[]。例如: env.addMapping(NAMESPACE, "Car", byte[].class, new MarshalBase64());

或者为您的类创建自定义编组器Car,它将写入此对象并将此编组器映射到 Car: env.addMapping(NAMESPACE, "Car", car.getClass(), new CustomMarshaller());

编辑:或者您可以手动编码byte[]base64字符串并StringgetProperty.

于 2013-07-29T06:53:09.243 回答