When to use Soapobject and SoapPrimitive
Asked Answered
C

1

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 :)

Costin answered 16/5, 2013 at 9:32 Comment(1)
SoapObject -> Used when the Reponse like Serilazed Class like Customer, Product......... SoapPrimitive-> used when the Response is like Primitive datatype like int, boolean , stringEnamel
E
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()));  
}
Enamel answered 16/5, 2013 at 9:37 Comment(4)
gr8..thnks... bt will u plz elaborate 'Serialized Class'Costin
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 applicationEnamel
Thats a nice answer and it should be marked as a correct answer. Straight to the point, i like it. @AkshayJoyMellen
@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?Yaws

© 2022 - 2024 — McMap. All rights reserved.