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

前端 未结 3 923
既然无缘
既然无缘 2021-01-07 14:29

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

相关标签:
3条回答
  • 2021-01-07 14:42

    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;
    }
    
    0 讨论(0)
  • 2021-01-07 14:43

    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.

    0 讨论(0)
  • 2021-01-07 14:46

    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;
    }
    
    0 讨论(0)
提交回复
热议问题