2012-03-29 64 views
1

我打电话给webservice使用KSoap2 in android,但我得到的响应如下。如何在android中解析这个ksoap响应。Android:如何解析KSOAP2响应

ResolveNamesResponse {ResponseMessages = anyType的{ResolveNamesResponseMessage = anyType的{=的MessageText多个被发现 结果.; ResponseCode = ErrorNameResolutionMultipleResults; DescriptiveLinkKey = 0; ResolutionSet = anyType {Resolution = anyType {Mailbox = anyType {Name = Amyj; [email protected]; RoutingType = SMTP; MailboxType =邮箱; };联系人= anyType {DisplayName =艾米约翰; GivenName = Amy; GivenName = Amy; EmailAddresses = anyType {Entry = SIP:[email protected]; Entry = SMTP:[email protected]; }; PhysicalAddresses = anyType {Entry = anyType {CountryOrRegion = China; }; }; ContactSource = ActiveDirectory;姓=约翰; }; }; Resolution = anyType {Mailbox = anyType {Name = Amyraj; [email protected]; RoutingType = SMTP; MailboxType =邮箱; }; Contact = anyType {DisplayName = Amy Raj; GivenName = Amy; GivenName = Amy; EmailAddresses = anyType {Entry = SIP:[email protected]; Entry = SMTP:[email protected]; }; PhysicalAddresses = anyType {Entry = anyType {CountryOrRegion = India; }; }; ContactSource = ActiveDirectory;姓=拉吉; }; }; Resolution = anyType {Mailbox = anyType {Name = shine; [email protected]; RoutingType = SMTP; MailboxType =邮箱; }; Contact = anyType {DisplayName = Shine Joseph; GivenName = Shine; GivenName = Shine; EmailAddresses = anyType {Entry = SIP:[email protected]; Entry = SMTP:[email protected]; }; PhysicalAddresses = anyType {Entry = anyType {CountryOrRegion = India; }; }; ContactSource = ActiveDirectory;姓=约瑟。 }; }; }; }; }; }

感谢您的帮助!

回答

-2

其实这个已知的格式,如果你知道Java Script。这些格式的数据实际上是JSON Object's and JSON Array's。因此,您可以通过这种方式解析这个结果。

如:

private Bundle bundleResult=new Bundle(); 
private JSONObject JSONObj; 
private JSONArray JSONArr; 
Private SoapObject resultSOAP = (SoapObject) envelope.getResponse(); 
/* gets our result in JSON String */ 
private String ResultObject = resultSOAP.getProperty(0).toString(); 

if (ResultObject.startsWith("{")) { // if JSON string is an object 
    JSONObj = new JSONObject(ResultObject); 
    Iterator<String> itr = JSONObj.keys(); 
    while (itr.hasNext()) { 
     String Key = (String) itr.next(); 
     String Value = JSONObj.getString(Key); 
     bundleResult.putString(Key, Value); 
     // System.out.println(bundleResult.getString(Key)); 
    } 
} else if (ResultObject.startsWith("[")) { // if JSON string is an array 
    JSONArr = new JSONArray(ResultObject); 
    System.out.println("length" + JSONArr.length()); 
    for (int i = 0; i < JSONArr.length(); i++) { 
     JSONObj = (JSONObject) JSONArr.get(i); 
     bundleResult.putString(String.valueOf(i), JSONObj.toString()); 
     // System.out.println(bundleResult.getString(i)); 
    } 
} 

我希望这可以帮助您解决问题。

+1

参考:http://stackoverflow.com/a/2049156/442580 – capdragon 2012-12-29 17:52:18

1

试试这个,我认为它会工作

SoapObject response = (SoapObject) envelope.getResponse(); 

int cols = response.getPropertyCount(); 

for (int i = 0; i < cols; i++) { 

    Object objectResponse = (Object) response.getProperty(i); 
    SoapObject r =(SoapObject) objectResponse; 

    String key1=(String) r.getProperty("key1").toString(); 

    // Get the rest of your Properties by 
    // (String) r.getProperty("PropertyName").toString();    
}