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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4951333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:45:41  来源:igfitidea点击:

Serialize an array of ints to send using KSOAP2

javaandroidweb-servicesksoap2android-ksoap2

提问by TomaszRykala

I'm having a problem trying to send an array of ints to a .NET web service which expects an array in one of the arguments. That's at least what I understand from the API description on the web service which says this:

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

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

So when I send a single int like below I do not get any errors and I think it works fine.

所以当我发送一个像下面这样的 int 时,我没有收到任何错误,我认为它工作正常。

request.addProperty("dataIndexIDs", 63);

But when I try to send an array of ints:

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

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

or a ArrayList of Integers:

或整数的 ArrayList:

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

I get thrown a "java.lang.RuntimeException: Cannot serialize" exception. Any help please? What am I doing wrong? Thanks!

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

采纳答案by TomaszRykala

It is a known issue with the KSOAP2 for Android library, which at the moment simply does not support arrays. The issue description is here:

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

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

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

A third-party patch, solution and an example can be found here:

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

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

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

I personally haven't tested any of them as they require also changing the web service WSDL but apparently they address the issue.

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

回答by user1691694

I'm sending from an Android client to a .NET server, this worked for me

我从 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);

Produces

生产

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

Which looks terrible, but the server eats it anyway

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

回答by Mindaugas Jaraminas

Here is nice example that might help you:

这是一个很好的例子,可以帮助你:

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

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

Here is my quick fix to this issue:

这是我对此问题的快速修复:

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);

Output XML:

输出 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>

回答by Lincy

Thanks Mindaugas. A slight edit to your answer worked for me:

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

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

回答by SANAT

Just pass parameter name and value like this in loop :

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

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);

Just pass itemId as parameter name in loop and value in loop.

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

回答by Ricardo Padilla

For me this works passing the Array elements one by one in a loop

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

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

回答by ?a?r? Erimez

For me this works

对我来说这有效

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());