Search code examples
androidksoap2

Parsing property name and value from ksoap response


So I've got my WebService connected to my Android app without any problems, and can get responses without any problems but I wanted to know if there's any way to get the Property name of the SoapObject as well as the value, instead of just getting the value?

For example I get this response (this is a very simple response just for a sample)

anyType{AsOfDate=2014-02-12T12:58:27-08:00; StatusID=9; }

Now when parsing I wanted to be able to take the values from my response and then create a List (like a NSDictionary) so I can have a key/value pair like :

AsOfDate = 2014-02-12T12:58:27-08:00;
StatusID = 9;

But so far I haven't been able to narrow down how I can get the property name when parsing, here's what I have so far:

SoapObject errortable = (SoapObject)dataset.getProperty("ErrorTable");
                for (int i = 0; i < errortable.getPropertyCount(); i++) {
                    Object object = errortable.getProperty(i);
                    //Log.d(ENSI_DEBUG, "the object is: " + object.getClass());
                    if (object instanceof SoapObject) {
                        Log.d(ENSI_DEBUG, "the object is a soap object = " + object);
                    }
                    if (object instanceof SoapPrimitive) {
                        Log.d(ENSI_DEBUG, "the object contains value: " + object.toString() + " and name: " //here's where I would like to get the property name to create the key/value pair );
                    }
                }

Solution

  • I figured out the problem, I needed to use the Property Info object to get the name, so I ended up using this code below to get what I needed.

    SoapObject result = (SoapObject) response.getProperty("result");
            SoapObject diffgram = (SoapObject) result.getProperty("diffgram");
            SoapObject dataset = (SoapObject) diffgram.getProperty("NewDataSet");
            for (int j = 0; j < dataset.getPropertyCount(); j++) {
                SoapObject finalObject = (SoapObject) dataset.getProperty(j);
                for (int i = 0; i < finalObject.getPropertyCount(); i++) {
                    Object object = finalObject.getProperty(i);
                    PropertyInfo propertyInfo = new PropertyInfo();
                    finalObject.getPropertyInfo(i, propertyInfo);
                    if (object instanceof SoapPrimitive) {
                        hashMap.put(propertyInfo.name, object.toString());
                    }
                }
                dataList.add(hashMap);
            }