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

Tom*_*ala 5 java android web-services ksoap2 android-ksoap2

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

<dataIndexIDs>
<int>int</int>
<int>int</int> </dataIndexIDs>
Run Code Online (Sandbox Code Playgroud)

所以当我发送一个如下所示的int时,我没有得到任何错误,我认为它工作正常.

request.addProperty("dataIndexIDs", 63);
Run Code Online (Sandbox Code Playgroud)

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

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

或者整数的ArrayList:

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

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

use*_*694 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);
Run Code Online (Sandbox Code Playgroud)

产生

 <classificationIds>
     <n4:short i:type="d:long" xmlns:n4="http://schemas.microsoft.com/2003/10/Serialization/Arrays">18</n4:short>
 </classificationIds>
Run Code Online (Sandbox Code Playgroud)

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


Min*_*nas 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);
Run Code Online (Sandbox Code Playgroud)

输出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>
Run Code Online (Sandbox Code Playgroud)


Tom*_*ala 2

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

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

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

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

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

  • 这个问题早已得到解决,最近的版本支持该行为,尽管没有使用链接的补丁,因为它从未使用 ksaop2-android 版本进行编译,也从未由提交者更新。 (3认同)