Android的web服务ksoap2 - 字节[]内POJO错误 [英] Android webservice ksoap2 - byte[] inside pojo Error

查看:175
本文介绍了Android的web服务ksoap2 - 字节[]内POJO错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用KVM系列化我的Andr​​oid应用程序调用Java web服务的肥皂。我的POJO类和code在下面给出

I am invoking java soap webservices from my android application using kvm serialization. my pojo classes and code is given below,

DataSet.java

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

and 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的,

In this I stored the images and name into the Photo.java POJO and then set into the DataSet POJO as,

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

But I am getting the following error Cannot serialize: [Lcom.common.Photo;@41221270.

什么是错的IM我的code。我已搜查这个问题。但余did't得到正确的答案。
 谁能帮我解决这个问题?

What is wrong im my code. I have searched about this issue. But I did't get correct answer. Can anyone help me to fix this?

推荐答案

在你的照片类的为getPropertyInfo方法,对数据属性的类型从

In the getPropertyInfo method of your Photo class, change the type for the data property from

字节[]。类

MarshalBase64.BYTE_ARRAY_CLASS

在的setProperty的赋值更改为

Change the value assignment in setProperty to

数据= Base64.de code(value.toString(),Base64.DEFAULT);

编辑回应您的评论:

您还需要创建一个PhotoList类:

You also need to create a PhotoList class:

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方法。

Change your DataSet class implementation so images is of type PhotoList and initialize it. Also change the types in your getPropertyInfo/setProperty methods.

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

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

You will also need to add the PhotoList mapping to your SOAP envelope.

这篇关于Android的web服务ksoap2 - 字节[]内POJO错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆