Search code examples
androidstringsoapcastingksoap2

How can I cast a String to a SoapObject in ksoap2?


In my Android app I use ksoap2 for communication with a server. I download a certain complex sports information structure via soap request and parse it later in my program.

What I want to do is somehow "save" this complex structure so that I can parse it again later when all the information in that structure is final and doesn't change anymore so that I can display it without calling the web service again.

I got that far that I can turn the whole structure into a string with the toString() method from the SoapObject class so that I can save it in the app's Shared preferences.

BUT: To parse the the string with my parser that I use when I download the data directly from the web server I need it as a SoapObject. I tried something like this:

SoapObject soapObj = (SoapObject) dataAsStringFromSharedPrefs;

But I cannot cast a string to a SoapObject that way.


Solution

  • This here seems to work:

    public SoapObject createSoapObjectFromSoapObjectString(String soapObjectString)
    {
    // Create a SoapSerializationEnvelope with some config
    SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    env.dotNet = true;
    
    // Set your string as output
    env.setOutputSoapObject(soapObjectString);
    
    // Get response
    SoapObject so = (SoapObject) env.getResponse();
    
    return so;
    }