序列化要使用KSOAP2发送的int数组

时间:2011-02-09 22:31:30

标签: java android web-services ksoap2 android-ksoap2

我在尝试向.NET Web服务发送一个int数组时遇到问题,该服务需要在其中一个参数中使用数组。至少我从Web服务的API描述中理解了这一点:

<dataIndexIDs>
<int>int</int>
<int>int</int> </dataIndexIDs>

因此,当我发送如下单个int时,我没有收到任何错误,我认为它工作正常。

request.addProperty("dataIndexIDs", 63);

但是当我尝试发送一组int时:

request.addProperty("dataIndexIDs", new int[] {63, 62}); // array of ints

或整数的ArrayList:

ArrayList<Integer> indexes = new ArrayList<Integer>();
    indexes.add(63);
    indexes.add(62);
    request.addProperty("dataIndexIDs", indexes); // ArrayList of Integers

我被抛出“java.lang.RuntimeException:无法序列化”异常。有什么帮助吗?我究竟做错了什么?谢谢!

7 个答案:

答案 0 :(得分:6)

我从Android客户端发送到.NET服务器,这对我有用

SoapObject myArrayParameter = new SoapObject(NAMESPACE, MY_ARRAY_PARAM_NAME);
for( int i : myArray ) {
    PropertyInfo p = new PropertyInfo();
    p.setNamespace("http://schemas.microsoft.com/2003/10/Serialization/Arrays");
    // use whatever type the server is expecting here (eg. "int")
    p.setName("short");
    p.setValue(i);
    myArrayParameter.addProperty(p);
}
request.addSoapObject(myArrayParameter);

可生产

 <classificationIds>
     <n4:short i:type="d:long" xmlns:n4="http://schemas.microsoft.com/2003/10/Serialization/Arrays">18</n4:short>
 </classificationIds>

哪个看起来很糟糕,但服务器无论如何都要吃掉它

答案 1 :(得分:5)

以下是可以帮助您的好例子:

http://code.google.com/p/ksoap2-android/wiki/CodingTipsAndTricks

以下是我对此问题的快速解决方法:

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
soapEnvelope.setOutputSoapObject(Request);
soapEnvelope.dotNet = true;


List<Integer> companies =  new ArrayList<Integer>();
companies.add(65);
companies.add(66);
companies.add(67);

Request.addProperty("name", "test1");
SoapObject soapCompanies = new SoapObject(NAMESPACE, "companies");
for (Integer i : companies){
    soapCompanies.addProperty("int", i);
}
Request.addSoapObject(soapCompanies);

输出XML:

<n0:companies xmlns:n0 = "http://tempuri.org/">
            <int i:type = "d:int">65</int>
            <int i:type = "d:int">66</int>
            <int i:type = "d:int">67</int>
</n0:companies>
<name i:type = "d:string">test1</name>

答案 2 :(得分:2)

这是KSOAP2 for Android库的一个已知问题,目前它只是不支持数组。问题描述如下:

http://code.google.com/p/ksoap2-android/issues/detail?id=19

可在此处找到第三方补丁,解决方案和示例:

http://people.unica.it/bart/ksoap2-patch/

我个人没有测试过任何一个,因为他们还需要更改Web服务WSDL,但显然他们解决了这个问题。

答案 3 :(得分:1)

感谢明道加斯。对你的回答略有编辑对我有用:

SoapObject soapObj = new SoapObject(namespace, "ImageId");
        for (Integer i : image_id){
            soapObj.addProperty("int", i);
        }
        request_login.addProperty("ImageId", soapObj);

答案 4 :(得分:1)

只需在循环中传递参数名称和值:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(URL);
for (int i=0; i<itemId.length; i++){
    request.addProperty("itemId",itemId[i]);
}
ht.call(SOAP_ACTION, envelope);

只需将itemId作为循环中的参数名称和循环中的值传递。

答案 5 :(得分:1)

对我来说,这可以在循环中逐个传递数组元素

public List<Integer> intsEnvio;
for(int i: intsEnvio){
    request.addProperty("clases", i);
}

答案 6 :(得分:0)

对我来说这有效

SoapObject soapCompanies = new SoapObject(NAMESPACE, "listcardIDs");
            for (int i=0;i<passed.getListCardIds().size()-1;i++) {
                soapCompanies.addProperty("int", passed.getListCardIds().get(i));
            }
            soapObject.addSoapObject(soapCompanies);
            //soapObject.addPropertyIfValue("listCardIDs", soapCompanies);
            soapObject.addProperty("userID", passed.getuserId());
            soapObject.addProperty("gender", passed.isgender());