序列化整数[]

时间:2013-06-17 13:41:47

标签: java arrays soap integer

我有Android APP和SOAPWebservice。我尝试在他们的整数数组之间发送,但Android Logcat说:

06-17 15:32:34.355: W/System.err(3107): java.lang.RuntimeException: Cannot serialize: [Ljava.lang.Integer;@42046ff8
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:664)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeProperty(SoapSerializationEnvelope.java:649)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:595)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeObjectBody(SoapSerializationEnvelope.java:573)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeElement(SoapSerializationEnvelope.java:658)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.serialization.SoapSerializationEnvelope.writeBody(SoapSerializationEnvelope.java:555)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.SoapEnvelope.write  (SoapEnvelope.java:205)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.transport.Transport.createRequestData(Transport.java:134)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:144)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
06-17 15:32:34.365: W/System.err(3107):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)

如果我无法对数据进行序列化,如何发送一堆整数?

ANDROID CODE(AsyncTask)

@Override
protected Integer doInBackground(Object... params) {

    try {       

        OSS_APPServiceCredentials wsc = new OSS_APPServiceCredentials("startUpdate");

        request = new SoapObject(wsc.init().get(1), wsc.init().get(0));

        request.addProperty("ParameterA", (String)params[0]);           
        request.addProperty("ParameterB", (Integer[])params[1]);

        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        OSS_PermitAllSSLConnections.allowAllSSL();
        KeepAliveHttpsTransportSE androidHttpTransport = new KeepAliveHttpsTransportSE(wsc.init().get(3),Integer.parseInt(wsc.init().get(5)), wsc.init().get(4), Integer.parseInt(wsc.init().get(6)));

        androidHttpTransport.call(wsc.init().get(2), envelope); 
        Object result = envelope.getResponse();
        Integer res = Integer.parseInt(result.toString());

        return res;
    } 

    catch (Exception E) {
        E.printStackTrace();
    }
    return null;
}

谢谢!

修改

我试着这样做:

BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = decoder.decodeBuffer(permissions);

int[] a = new int[bytes.length / 4];
ByteBuffer.wrap(bytes).asIntBuffer().get(a);

但Eclipse(在WebService中)说出这个错误,我不知道为什么!

The method wrap(byte[]) is undefined for the type ByteBuffer

修改

最后我解决了这个问题。只有我需要导入:

import java.nio.ByteBuffer;

1 个答案:

答案 0 :(得分:4)

您可以使用ByteBuffer

将int []转换为byte []
    int[] a = { 1, 2, 3 };
    ByteBuffer bb = ByteBuffer.allocate(a.length * 4);
    bb.asIntBuffer().put(a);
    byte[] bytes = bb1.array();

将byte []发送到服务器并将其转换回int []

    int[] a = new int[bytes.length / 4];
    ByteBuffer.wrap(bytes).asIntBuffer().get(a);