How to pass String array to webservice using ksoap2?

11,661

First use "soapUI" to see correct request structure(like item names,item namespaces , ...). We assume that you want to write like this XML in request:(here n0 and n1 are namespaces)

<n0:strarray xmlns:n0="http://n0 ..." xmlns:n1="http://n1 ...">
        <n1:string>hello</n1:string>
        <n1:string>world</n1:string>
</n0:strarray>

extend a class from vector:

import java.util.Hashtable;
import java.util.Vector;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class StringArraySerializer extends Vector<String> implements KvmSerializable {
      //n1 stores item namespaces:
    String n1 = "http://n1 ...";

        @Override
        public Object getProperty(int arg0) {
                return this.get(arg0);
        }

        @Override
        public int getPropertyCount() {
                return this.size();
        }

        @Override
        public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
                arg2.setName = "string";
                arg2.type = PropertyInfo.STRING_CLASS;
            arg2.setNamespace = n1;
        }

        @Override
        public void setProperty(int arg0, Object arg1) {
                this.add(arg1.toString());
        }

}

To build the request you have to do this:

1-make a new Vector-Object from this class:

StringArraySerializer stringArray = new StringArraySerializer();

2-then you can add elements:

stringArray.add("hello");
stringArray.add("world");

3-then you create a PropertyInfo with it:

//n0 stores array namespace:
String n0 = "http://n0 ...";
stringArrayProperty = new PropertyInfo();
stringArrayProperty.setName("strarray");
stringArrayProperty.setValue(stringArray);
stringArrayProperty.setType(stringArray.getClass());
stringArrayProperty.setNamespace(n0);

4-then you add all the properties to the request:

Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty(stringArrayProperty);

Reference:

ksoap2-android,CodingTipsAndTricks

Share:
11,661
Admin
Author by

Admin

Updated on June 20, 2022

Comments

  • Admin
    Admin almost 2 years

    I am having a Web Client in Android using ksoap2 but I can't pass the string array as a parameter to the webservice.

    Here's my code

    String[] items={"hello","world"};
    request.addproperty("str",items);
    
  • Tim Post
    Tim Post over 12 years
    You need to summarize those examples in your answer, or it will likely be removed. If the link breaks, so does this answer :)
  • Vivek Singh
    Vivek Singh almost 11 years
    What namespace i have to put in n1 & n0 ??
  • hasanghaforian
    hasanghaforian almost 11 years
    @VivekSingh Easiest way is this:Use SoapUI and see the correct request.You can find namespaces there.
  • Harshal Kalavadiya
    Harshal Kalavadiya over 10 years
    @hasanghaforian:i need your help pls. refer this link http://stackoverflow.com/questions/19198017/pass-arraylist-d‌​ata-into-soap-web-se‌​rvice-in-android how cani pass array data to SOAP service?
  • Saeid
    Saeid almost 10 years
    this way are suitable for HashMap too?
  • hasanghaforian
    hasanghaforian almost 10 years
    @saeid.I do not try HashMap,but if you read the reference that is at the end of answer,you can see that it is possible.