1

I am consuming a SOAP based web-service using ksoap2, and receiving a JSON response from the webservice, which I am storing in a SoapObject.

The next step is to parse the JSON. For that I am trying to get each property from the response SoapObject and store it in another SoapObject, for further processing (like getting the name and value etc.)

SoapObject soapObjectEach= (SoapObject) postResult.getProperty(i); 

But I am getting an exception at this statement: java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive cannot be cast to org.ksoap2.serialization.SoapObject

postResult is a SoapOject. This suggests me that postResult.getProperty() probably returns a SoapPrimitive? But I am almost certain that it is not the case, as the documentation says, "Returns the desired property"

So can anybody suggest something about it? I have seen other questions about this (1, 2) but did not get to a satisfactory answer.

Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183

1 Answers1

1

Lets diagonize your code:

SoapObject soapObjectEach= (SoapObject) postResult.getProperty(i); 

As you said postResult is a soapobject. And from the documentation on getproperty says:

getProperty

public java.lang.Object getProperty(int index)
Returns a specific property at a certain index.
Specified by:
getProperty in interface KvmSerializable
Parameters:
index - the index of the desired property
Returns:
the desired property

The getproperty returns the property at the index and not something that can be always be cast into a soapobject. And from your exception we can see it is returning an object of type soapprimitive. So the solution would be something similar to this post.

Community
  • 1
  • 1
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • Thank you very much. That solved the problem. But that answer does not state why or how. So can you tell me if you know that? – Solace Jun 08 '14 at 06:22
  • its simple soapprimitive cannot be cast into soapobject http://stackoverflow.com/questions/840322/how-does-the-java-cast-operator-work this link explains what you need – Illegal Argument Jun 08 '14 at 06:28