5

Calling a .net SOAP1.1 web service from android using ksoap2 lib I met a problem of casting response to a custom object. For instance the code below is called correct after httpTransport.call(soapAction, soapEnvelope); and have data inside. But I cant't cast it to specific object neither to SoapObject or Vector as I saw in several examples, I get CastException or simple nothing. If somebody knows how to deal with it, please help.

public StatusSetting[] GetAllStatuses(String installation){
    StatusSetting[] statuses = null;
    String methodName =  "GetAllStatuses";
    String soapAction = NAMESPACE + "/" + methodName;
    SoapObject request = new SoapObject(NAMESPACE, methodName);
    request.addProperty("installation", installation);

    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.dotNet = true;
    soapEnvelope.setOutputSoapObject(request);

    AndroidHttpTransport httpTransport = new AndroidHttpTransport(SERVICE_URL);
    try {
        httpTransport.call(soapAction, soapEnvelope);
        statuses = (StatusSetting[])soapEnvelope.getResponse();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return statuses;
}
Maxim
  • 4,152
  • 8
  • 50
  • 77

3 Answers3

5

First try and see if you are getting any response.

Object obj = envelope.bodyIn; 

if this obj is not null then try the following.

SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;

My guess is this should work if you are getting some response.

You need to tell kSOAP what StatusSetting object is and how to convert a SOAP response to to a StatusSetting object.

Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161
  • Thanks, it works, I have data in the SoapObject. Could you please give a direction how to parse SoapObject. I mean read properties somehow and look for a data I need. I tried to get resultsRequestSOAP.properties it works (it gets like json data) in debug mode but properties is not open method to use in code. – Maxim Jun 15 '10 at 09:32
  • I know of two ways to parse the SoapObject. 1. Implement the `org.ksoap2.serialization.Marshal` interface and register for the corresponding SOAP tag. 2. Another way is to use - the `getProperty()` and `getAttribute()` methods of SoapObject to get teh corresponding values. HTH. – Soumya Simanta Jun 15 '10 at 14:15
  • Also look at this. http://www.drdobbs.com/article/printableArticle.jhtml;jsessionid=OKCYS4LV2OQDPQE1GHPCKHWATMY32JVN?articleId=208800166&dept_url=/mobility/ – Soumya Simanta Jun 15 '10 at 14:17
1

I hope this will be helpful for you:

SoapObject result = (SoapObject) envelope.bodyIn;
String response = result.getProperty(0).toString();
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
Anamika
  • 99
  • 1
  • 1
  • 6
0

something like that:

SoapObject response = (SoapObject) envelope.getResponse();
   yourArray=new String[response.getPropertyCount()];

   for(int i=0;i<response.getPropertyCount();i++){    
       Object property = response.getProperty(i);
       if(property instanceof SoapObject){
           SoapObject final_object = (SoapObject) property;
           yourArray[i] = final_object.getProperty("YOUR_PROPERTY_NAME");
    }
}
Melih Mucuk
  • 6,988
  • 6
  • 37
  • 56