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

随声附和 提交于 2019-12-09 01:59:23

问题


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.


回答1:


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.




回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/13401715/how-can-i-cast-a-string-to-a-soapobject-in-ksoap2

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