如何解析Android中的Soap响应返回Map(键和值对)?

时间:2012-11-08 08:01:06

标签: android web-services parsing soap

我只是对一个Web服务进行Soap调用,并在一个字符串中给出响应,其中包含如下所示的数据:

    [Map{
         item=anyType{key=customer_id; value=1; }; 
         item=anyType{key=created_at; value=2007-08-30 23:23:13; }; 
         item=anyType{key=updated_at; value=2008-08-08 12:28:24; }; 
         item=anyType{key=increment_id; value=000000001; }; 
         item=anyType{key=store_id; value=1; }; 
         item=anyType{key=website_id; value=1; }; 
         item=anyType{key=default_billing; value=274; }; 
         item=anyType{key=default_shipping; value=274; }; 
         item=anyType{key=disable_auto_group_change; value=0; }; 
         item=anyType{key=email; value=john.doe@example.com; }; 
         item=anyType{key=firstname; value=John; }; 
         item=anyType{key=group_id; value=1; }; 
         item=anyType{key=lastname; value=Doe; }; 
         item=anyType{key=middlename; value=; }; 
         item=anyType{key=password_hash; value=2049484a4020ed15d0e4238db22977d5:eg; }; 
         item=anyType{key=prefix; value=; }; 
         item=anyType{key=suffix; value=; }; 
         item=anyType{key=taxvat; value=; };

}]

所以我只是尝试了很多资源来解析这个响应,比如JSON,Vector,Enumeration,Map,HashMap .....但我无法解析这个响应,

这不是JSON响应,但我仍尝试用JSON解析数据,但我不能......

解析此类响应的最佳方法是什么?

有人知道怎么做吗?

感谢......

1 个答案:

答案 0 :(得分:1)

您可以像这样创建一个新类 SoapMap

class SoapMap
{
    SoapObject soapObject;
    Map<String, String> pairs = new HashMap<String, String>();

    public SoapMap(SoapObject soapObject)
    {
        this.soapObject = soapObject;
        for(int i=0; i<soapObject.getPropertyCount(); i++)
        {
            String str = soapObject.getProperty(i).toString();
            String key = str.split("key=")[1].split(";")[0];
            String value = str.split("value=")[1].split(";")[0];            
            pairs.put(key, value);
        }
    }

    public String getValue(String key)
    {
        return pairs.get(key);
    }
}

并使用SoapMap,如:

SoapMap soapMap = new SoapMap(yourSoapObjectResponse);
int customer_id = Integer.parseInt( soapMap.getValue("customer_id") );
String firstname = soapMap.getValue("firstname");
String email = soapMap.getValue("email");