2012-03-29 63 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();    
}