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

问题描述 投票:5回答:7

我在尝试向.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:无法序列化”异常。有什么帮助吗?我究竟做错了什么?谢谢!

java android web-services ksoap2 android-ksoap2
7个回答
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>

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


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
投票

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

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

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

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

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


1
投票

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

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

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作为循环中的参数名称和循环中的值传递即可。


1
投票

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

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

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());
© www.soinside.com 2019 - 2024. All rights reserved.