Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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;
}

Solution 2:

Hope this should work fine for converting a soap XML string to a SoapObject

public SoapObject string2SoapObject(byte[] bytes)
{
    SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER12);
    SoapObject soap=null;
    try {
        ByteArrayInputStream inputStream=new ByteArrayInputStream(bytes);
        XmlPullParser p= Xml.newPullParser();
        p.setInput(inputStream, "UTF8");
        envelope.parse(p);
        soap=(SoapObject)envelope.bodyIn;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return soap;
}

Solution 3:

Lets say, someObject is an object with the members CategoryId, Name, Description. If you are getting these object members in the response, you can save them to someObject by doing this:

SoapObject response = (SoapObject)envelope.getResponse();

someObject.CategoryId =  Integer.parseInt(response.getProperty(0).toString());
someObject.Name =  response.getProperty(1).toString();
someObject.Description = response.getProperty(2).toString();

EDIT:

Ok I see the problem now.

To get a soapobject, only way I can think of is:

1)parse the stored string 2)store all the data fields in local variables

Parse stored string:

start loop
int x = something
string y = something
double z = something
end loop

3)create a new object using the variables

someObject.fieldx = x
someObject.fieldy = y
someObject.fieldz = z

4)create a new soapobject

SoapObject sp_Object = new SoapObject(NAMESPACE, METHOD_NAME);

5)create a propertyinfo using the object in step 3

PropertyInfo prop = new PropertyInfo();
prop.setNamespace(NAMESPACE);
prop.setType(someObject.getClass());
prop.setValue(someObject);

6)add the propertyinfo to the soapobject in step 4

sp_Object.addProperty(prop);

Then you can use the soapobject sp_Object for your parser.


Post a Comment for "How Can I Cast A String To A SoapObject In Ksoap2?"