5

我在尝试将整数数组发送到 .NET Web 服务时遇到问题,该服务需要一个参数中的数组。这至少是我从 Web 服务上的 API 描述中所理解的,它是这样说的:

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

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

request.addProperty("dataIndexIDs", 63);

但是当我尝试发送一个整数数组时:

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

4

7 回答 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>

看起来很糟糕,但服务器还是吃掉了它

于 2012-12-12T01:29:44.340 回答
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>
于 2012-03-01T08:14:00.763 回答
2

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

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

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

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

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

于 2011-03-13T15:14:49.963 回答
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 作为循环中的参数名称和循环中的值传递。

于 2014-02-05T12:36:10.957 回答
1

谢谢明道加斯。对您的回答稍作修改对我有用:

SoapObject soapObj = new SoapObject(namespace, "ImageId");
        for (Integer i : image_id){
            soapObj.addProperty("int", i);
        }
        request_login.addProperty("ImageId", soapObj);
于 2013-03-20T04:35:05.120 回答
1

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

public List<Integer> intsEnvio;
for(int i: intsEnvio){
    request.addProperty("clases", i);
}
于 2015-06-29T22:20:02.050 回答
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());
于 2019-04-27T10:48:28.893 回答