Android webservice ksoap2 - 在pojo内部的byte []错误

时间:2012-08-28 05:02:17

标签: android ksoap2

我正在使用kvm序列化从我的android应用程序调用java soap webservices。我的pojo类和代码如下所示,

DataSet.java

public class DataSet implements KvmSerializable{

public String client = null;
public Photo[] images;
public String userId = null;

@Override
public Object getProperty(int arg0) {
    switch (arg0){
    case 0:
        return client;      
    case 1:
        return images;
    case 2:
        return userId;
     default:
         return null;
    }
}

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

@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
    case 0:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "client";
        break;        
    case 1:
        info.type = Photo.class;
        info.name = "images";
    case 2:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "userId";
    default:
        break;
    }

}

@Override
public void setProperty(int index, Object value) {
    switch(index)
    {
    case 0:
        client = value.toString();
        break;
    case 1:
        images = (Photo[]) value;
        break;
    case 2:
        userId = value.toString();
        break;
    default:
        break;
    }

}

}

和Photo.java

public class Photo implements KvmSerializable{

public byte[] data;
public String name;

@Override
public Object getProperty(int arg0) {
    switch (arg0){
    case 0:
        return data;        
    case 1:
        return name;        
     default:
         return null;
    }
}

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

@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
    switch(index)
    {
    case 0:
        info.type = byte[].class;
        info.name = "data";
        break;        
    case 1:
        info.type = PropertyInfo.STRING_CLASS;
        info.name = "name";
    default:
        break;
    }

}

@Override
public void setProperty(int index, Object value) {
    switch(index)
    {
    case 0:
        data = (byte[])value;
        break;
    case 1:
        name = value.toString();
        break;
    default:
        break;
    }

}

在此我将图像和名称存储到Photo.java POJO中,然后将其设置为DataSet POJO,

public class Sample implements Runnable {
    ...
    ...
    Photo[] photoArr = new Photo[3];
    int count = 0;

public void run() {
  while (count < 3) {
    ....
    ....
    ....
    byte[] byteArray = stream.toByteArray();
    Photo photo = new Photo();
    photo.data = byteArray;
    photo.name = "img"+count;
    photoArr[count] = photo;
    count++;
 }
       if(count == 2)
         {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            new MarshalBase64().register(envelope);   //serialization
            envelope.encodingStyle = SoapEnvelope.ENC;


            PropertyInfo pi = new PropertyInfo();
            DataSet dataSet = new DataSet();
            dataSet.client = "1";
            dataSet.userId = "1";
            dataSet.images = photoArr;

            pi.setName("dataSet");
            pi.setValue(dataSet);
            pi.setType(dataSet.getClass());
            request.addProperty(pi);

            envelope.setOutputSoapObject(request); 
            envelope.addMapping(NAMESPACE, "dataSet", DataSet.class);
            envelope.addMapping(NAMESPACE, "images", Photo.class);

            AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
            httpTransport.debug = true;                         
            httpTransport.call(SOAP_ACTION, envelope);

            SoapObject result = (SoapObject) envelope.bodyIn;
        Log.d("Result: ", result.toString());
         }

}

但是我收到以下错误无法序列化:[Lcom.common.Photo; @ 41221270

我的代码有什么问题。我搜索过这个问题。但我没有得到正确答案。  任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

在Photo类的getPropertyInfo方法中,从

更改数据属性的类型

byte[].class

MarshalBase64.BYTE_ARRAY_CLASS

将setProperty中的值赋值更改为

data = Base64.decode(value.toString(), Base64.DEFAULT);

根据您的评论进行修改:

您还需要创建PhotoList类:

public class PhotoList extends Vector<Photo> implements
    KvmSerializable {

private static final long serialVersionUID = 12345L;  // you can let the IDE generate this

@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((Photo) value);
}

@Override
public void getPropertyInfo(int index, Hashtable properties,
        PropertyInfo info) {
    info.name = "Photo";
    info.type = Photo.class;
}
}

更改您的DataSet类实现,使图像的类型为PhotoList并初始化它。还要更改getPropertyInfo / setProperty方法中的类型。

PhotoList images = new PhotoList();
...
info.type = PhotoList.class;
...
images = (PhotoList)value;

您还需要将PhotoList映射添加到SOAP信封中。