从ksoap响应中解析属性名称和值

时间:2014-02-12 23:02:29

标签: android ksoap2

所以我的WebService连接到我的Android应用程序没有任何问题,并且可以毫无问题地获得响应,但我想知道是否有任何方法可以获取SoapObject的属性名称以及值,而是刚刚获得价值?

例如,我得到了这个响应(这只是一个非常简单的样本响应)

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

现在解析时我希望能够从我的响应中获取值,然后创建一个List(比如NSDictionary),这样我就可以拥有一个键/值对,如:

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

但到目前为止,我还没有能够缩小解析时如何获取属性名称,这是我到目前为止所拥有的:

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

2 个答案:

答案 0 :(得分:2)

我想出了问题,我需要使用Property Info对象来获取名称,所以我最终使用下面的代码来获取我需要的东西。

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

答案 1 :(得分:0)

您可以使用此示例代码。它适用于我。

           if (envelope.bodyIn instanceof SoapObject) { // SoapObject =
                                                            // SUCCESS
                SoapObject response = (SoapObject) envelope.getResponse();
                int count = response.getPropertyCount();
                Log.i("count", Integer.toString(count));

                int StatusID= Integer.parseInt(response
                        .getPropertyAsString("StatusID"));
                Log.i("StatusID", Integer.toString(StatusID));
                String AsOfDate= (response
                        .getPropertyAsString("AsOfDate"));
                Log.i("AsOfDate", AsOfDate);

            }