12

I have been working with ksoap2 lately.

I am still confused whether what is the EXACT difference between SoapObject and SoapPrimitive.

And when to use them.

I guess its something related to string and arrays. Is it true?

I found some links but got confused.

Can anyone tell me the difference and when to use which one in the simplest form of English?

Thanks :)

Shachi
  • 1,858
  • 3
  • 23
  • 41
  • 5
    SoapObject -> Used when the Reponse like Serilazed Class like Customer, Product......... SoapPrimitive-> used when the Response is like Primitive datatype like int, boolean , string – Akshay Joy May 16 '13 at 09:39

1 Answers1

19

SoapObject is used when we need to get the Response for a Class type, like Customer, Product, etc. (From the SoapObject you need to iterate over the values inside the SoapResponse.) SoapPrimitive is used for Primitive datatypes like Integer, Boolean.

For example, in the following code I am expecting a Boolean value from SoapResponse:

SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
Boolean status = Boolean.valueOf(response.toString());

And in the following code, I need to get the Response as an Object:

SoapObject response = (SoapObject) envelope.getResponse();
Log.d("Response", response.toString());
int count = response.getPropertyCount();

for (int i = 0; i < count; i++) {
    userObj = new User(response.getProperty(1).toString(),
                       Double.parseDouble(response.getProperty(2).toString()));  
}
Anis Abboud
  • 1,328
  • 2
  • 16
  • 23
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
  • gr8..thnks... bt will u plz elaborate 'Serialized Class' – Shachi May 16 '13 at 09:45
  • 2
    When u Create WebService If you want to Return the Response as customer Class, You neeed to make Customer class Serializable. then only we can consume the same in client application – Akshay Joy May 16 '13 at 09:48
  • Thats a nice answer and it should be marked as a correct answer. Straight to the point, i like it. @AkshayJoy – Sindri Þór Sep 08 '15 at 13:22
  • @AkshayJoy very aprreciative. I still need one clarification. With Reference to your above Ex.. Does this mean you have pre created a Class User(CustomerName, ProductNo) some where in your project, as you already know the return types of that WebService from the WSDL? Also, What if the response.getPropertyCount() returns 3, wont there be an array for userObj? And lastly, shouldnt start the index of response.getPropertyCount() with 0 instead of 1? – user3833732 Dec 11 '15 at 15:12