KSoap2 Android Receive Array of Objects

18,467

Solution 1

to get Multiple response just add few lines into your code instead of the ..........=envelope.getResponse();

SoapObject obj1 = (SoapObject) envelope.getResponse();

SoapObject obj2 =(SoapObject) obj1.getProperty(0);


for(int i=0; i<obj2.getPropertyCount(); i++)
{
   SoapObject obj3 =(SoapObject) obj2.getProperty(i);
   int id= Integer.parseInt(obj3.getProperty(0).toString());
   String start_date = obj3.getProperty(1).toString();
   String end_date = obj3.getProperty(2).toString();
   int id= Integer.parseInt(obj3.getProperty(3).toString());
}

If you still have any Problem you can write me.

Solution 2

I always get Vector as response type, so my solution is:

        HttpTransportSE conn = new HttpTransportSE(url);
        try{
            conn.call(soapAction, envelope); //send request
            Vector<SoapObject> result= (Vector<SoapObject>)envelope.getResponse();

            int length = result.size();
            for(int i=0;i<length; ++i){
                SoapObject so = result.get(i);
                Log.d(TAG,so.getPropertyAsString(3));
            }
        } catch(Exception e){
            e.printStackTrace();
        }
Share:
18,467
Swayam
Author by

Swayam

Updated on June 08, 2022

Comments

  • Swayam
    Swayam almost 2 years

    I am trying to use a .NET webservice in my application where the service returns an array of objects as a response.

    This is the format of the response from the web-service.

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <GetPickersResponse xmlns="http://tempuri.org/">
      <GetPickersResult>
        <Picker>
          <Id>int</Id>
          <StartTime>dateTime</StartTime>
          <EndTime>dateTime</EndTime>
          <PickerCount>int</PickerCount>
        </Picker>
        <Picker>
          <Id>int</Id>
          <StartTime>dateTime</StartTime>
          <EndTime>dateTime</EndTime>
          <PickerCount>int</PickerCount>
        </Picker>
      </GetPickersResult>
    </GetPickersResponse>
    </soap:Body>
    </soap:Envelope>
    

    This is my Java code to get the response from the web-service.

    SoapObject request = new SoapObject(NAMESPACE, METHOD_GET_CONTROL);
    SoapSerializationEnvelope envelope = 
            new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
    
    
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    
     try {
                 androidHttpTransport.call(SOAP_ACTION_GET_CONTROL, envelope);
                ..........=envelope.getResponse(); //To get the data. }
    

    My question, with what do I replace the "........" in my source code to receive the array of objects as a response from the service ? I need to receive multiple objects and then use their individual data members.

    Please help. I am new to Web-services and Ksoap.

  • Swayam
    Swayam about 11 years
    I actually already solved it using a modified version of Sachin D's solution, but nevertheless, thank you for your help. And +1 for the use of Vector. Thanks! :)
  • Pankaj Kakade
    Pankaj Kakade about 11 years
    hear i am passing two parameter and getting list of no. as well this code can use for accessing and displaying String.
  • SjoerdvGestel
    SjoerdvGestel about 9 years
    shouldn't it be: SoapObject obj3 =(SoapObject) obj2.getProperty(i); otherwise for each loop you pick position 0 from the second object instead of looping though its content