Karthik
Karthik

Reputation: 5033

How to pass String of array values to webservice using ksoap2?

I have a Web Client in Android using ksoap2 but I can't pass the string array as a parameter to the webservice. I have attached my code below and services.

Services:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mys="http://182.73.50.69:6510/Myservice" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays ">
       <soapenv:Header/>
       <soapenv:Body>
          <mys:addworkload>
             <!--Optional:-->
             <mys:member>Antony</mys:member>
             <!--Optional:-->
             <mys:prj>
                <!--Zero or more repetitions:-->
                <arr:string>Prj1</arr:string>
                <arr:string>Prj2</arr:string>
                <arr:string>Prj3</arr:string>
             </mys:prj>
             <!--Optional:-->
             <mys:val>
                <!--Zero or more repetitions:-->
                <arr:int>2</arr:int>
                <arr:int>2</arr:int>
                <arr:int>2</arr:int>
             </mys:val>
             <!--Optional:-->
             <mys:date>2016-05-29</mys:date>
          </mys:addworkload>
       </soapenv:Body>
    </soapenv:Envelope>

Android Code:

SoapObject Search= new SoapObject(Constants.NAMESPACE, "addworkload");
        List<String> companies =  new ArrayList<>();
        companies.add("Prj1");
        companies.add("Prj2");
        companies.add("Prj3");

        List<String> ones =  new ArrayList<>();
        ones.add("2");
        ones.add("2");
        ones.add("2");

        SoapObject soapCompanies = new SoapObject(Constants.NAMESPACE, "prj");
        for (String i : companies){
            soapCompanies.addProperty("string", i);
        }
        request.addSoapObject(soapCompanies);

        SoapObject val = new SoapObject(Constants.NAMESPACE, "val");
        for (String i : ones){
            val.addProperty("int", i);
        }
        request.addSoapObject(val);

Kindly tell me where I am missing the link here..

Upvotes: 0

Views: 1481

Answers (1)

Neerajlal K
Neerajlal K

Reputation: 6828

You will need two custom serializable classes that extends a collection of the type you want to hold. It should also implements KvmSerializable.

Class for String array,

public class CustomKvmSerializable extends Vector<String> implements KvmSerializable {

    private String tag;
    private Class klass;

    public CustomKvmSerializable(String tag, Class klass) {
        this.tag = tag;
        this.klass = klass;
    }

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

    @Override
    public int getPropertyCount() {
        return 1;
    }

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

    @Override
    public void getPropertyInfo(int i, Hashtable hashtable, PropertyInfo propertyInfo) {
        propertyInfo.name = tag;
        propertyInfo.type = klass;
    }

    @Override
    public String getInnerText() {
        return null;
    }

    @Override
    public void setInnerText(String s) {

    }
}

For integer array create a similar class extending Vector<Integer>.

To use this in your request,

ArrayList<PropertyInfo> props = new ArrayList<>();

// For string array
CustomKvmSerializable stringVector = new CustomKvmSerializable("string", String.class);
for(String str : yourStringArray){
    stringVector.add(str);
}
PropertyInfo instrumentInfo = new PropertyInfo();
instrumentInfo.setName("prj");
instrumentInfo.setValue(stringVector);
instrumentInfo.setType(stringVector.getClass());
props.add(instrumentInfo);

// For integer array
CustomKvmSerializable integerVector = new CustomKvmSerializable("int", String.class);
for(Integer integer : yourIntegerArray){
    integerVector.add(integer);
}
PropertyInfo instrumentInfo = new PropertyInfo();
instrumentInfo.setName("val");
instrumentInfo.setValue(integerVector);
instrumentInfo.setType(integerVector.getClass());
props.add(instrumentInfo);

When you create the request,

SoapObject search = new SoapObject(Constants.NAMESPACE, "addworkload");

// Add the properties to your request
for (PropertyInfo property : props) {
    request.addProperty(property);
}

Upvotes: 1

Related Questions