Search code examples
androidjsonserializationksoap2classcastexception

ClassCastException: SoapPrimitive can't be cast to SoapObject (Apparently there is no SoapPrimitive here)


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.


Solution

  • 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.