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

元气小坏坏 提交于 2019-12-01 01:56:16

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.

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!