Ksoap2 android库marshal byte []

时间:2013-12-10 16:26:01

标签: android web-services bytearray marshalling ksoap2

我试图通过webservices传递一个byte [],我需要编组byte []。到目前为止,这就是我所拥有的。这个方法在我创建的一个名为MarshallArray的类中实现Marshal

public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
    byte[] myArray = (byte[]) obj;  
    for (int i = 0; i < myArray.length; i++) {
        writer.startTag("", "ArrayOfBytes");
        writer.text(Base64.encode(myArray[i]).toString());
        writer.endTag("", "ArrayOfBytes");
    }
}

我用来创建它的例子是

public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
    String[][][] myArray = (String[][][]) obj;
    for (int i = 0; i < myArray.length; i++) {
        writer.startTag("", "ArrayOfArrayOfString");
        for (int j = 0; j < myArray[i].length; j++) {
            writer.startTag("", "ArrayOfString");
            for (int k = 0; k < myArray[i][j].length; k++) {
                writer.startTag("", "string");
                writer.text(myArray[i][j][k]);
                writer.endTag("", "string");
            }
            writer.endTag("", "ArrayOfString");
        }
        writer.endTag("", "ArrayOfArrayOfString");
    }
}

我遇到的问题是,第一种方法是如何访问byte []中的元素,因为它只能接收数组而不是字节中的元素[]

Base64.enconde(byte[])

1 个答案:

答案 0 :(得分:0)

通常我做的是创建一个新的SoapObject,如下所示:

SoapObject request = new SoapObject(...)

然后我按如下方式添加我的字节数组:

request.addProperty("myArray ", myArray );

然后:

SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
MarshalBase64().register(envelope);
envelope.setOutputSoapObject(request);