Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KSoap2 Android Receive Array of Objects

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.

like image 944
Swayam Avatar asked Dec 27 '22 23:12

Swayam


2 Answers

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.

like image 91
Sachin D Avatar answered Jan 09 '23 09:01

Sachin D


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();
        }
like image 23
andep Avatar answered Jan 09 '23 09:01

andep