Serialize an array of ints to send using KSOAP2
Asked Answered
C

7

5

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:

<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.

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<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!

Cavalryman answered 9/2, 2011 at 22:31 Comment(3)
I've got a feeling that actually even my single int attempt is not working properly, although not throwing exceptions because not just the datasource=63 is being returned.Cavalryman
It's definitely an array of ints that it wants but it looks like KSOAP2 has problems serializing it. I though that ints and arrays were serializable by default. please helpCavalryman
Honestly, noone can provide any help? :(Cavalryman
C
2

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:

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/

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

Cavalryman answered 13/3, 2011 at 15:14 Comment(1)
That issue has long been fixed and recent releases support that behaviour although not using the linked patch since that never compiled using the ksaop2-android build and was never updated by the submitter.Usn
V
6

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

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

Vehement answered 12/12, 2012 at 1:29 Comment(0)
S
5

Here is nice example that might help you:

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:

<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>
Stomachic answered 1/3, 2012 at 8:14 Comment(2)
Interesting. I will try this next time. Thanks for sharing!Cavalryman
this line is given error that the method doesn't exists Request.addSoapObject(soapCompanies); I am using KSOAP2Bowman
C
2

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:

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/

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

Cavalryman answered 13/3, 2011 at 15:14 Comment(1)
That issue has long been fixed and recent releases support that behaviour although not using the linked patch since that never compiled using the ksaop2-android build and was never updated by the submitter.Usn
T
1

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);
Tadich answered 20/3, 2013 at 4:35 Comment(0)
A
1

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.

Acrimonious answered 5/2, 2014 at 12:36 Comment(0)
D
1

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);
}
Doggett answered 29/6, 2015 at 22:20 Comment(0)
B
0

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());
Box answered 27/4, 2019 at 10:48 Comment(1)
Welcome to SO. It's useful to add an explanation of how your code addresses the OP's question.Appearance

© 2022 - 2024 — McMap. All rights reserved.