我在Android中使用KSoap2调用了一个web服务,但我得到的响应如下。如何在Android中解析这个ksoap响应?
ResolveNamesResponse {ResponseMessages = anyType {ResolveNamesResponseMessage = anyType {MessageText =找到多个结果。 ResponseCode = ErrorNameResolutionMultipleResults; DescriptiveLinkKey = 0; ResolutionSet = anyType的{分辨率= anyType的{邮箱= anyType的{名称= Amyj; [email protected]; RoutingType = SMTP; MailboxType =邮箱; }; Contact = anyType {DisplayName = Amy John;给定名称=艾米; EmailAddresses = anyType的{条目= SIP:[email protected];条目= SMTP:[email protected]; }; PhysicalAddresses = {anyType的输入= {anyType的=的CountryOrRegion中国; }; }; ContactSource = ActiveDirectory的;姓=约翰; }; };分辨率= {anyType的邮箱= anyType的{名称= Amyraj; [email protected]; RoutingType = SMTP; MailboxType =邮箱; }; Contact = anyType {DisplayName = Amy Raj;给定名称=艾米; EmailAddresses = anyType的{条目= SIP:[email protected];条目= SMTP:[email protected]; }; PhysicalAddresses = anyType的{输入= anyType的{的CountryOrRegion =印度; }; }; ContactSource = ActiveDirectory的;姓=拉吉; }; };分辨率= anyType的{邮箱= anyType的{名称=光泽; [email protected]; RoutingType = SMTP; MailboxType =邮箱; }; Contact = anyType {DisplayName = Shine Joseph;给定名称=服务; EmailAddresses = anyType的{条目= SIP:[email protected];条目= SMTP:[email protected]; }; PhysicalAddresses = {anyType的输入= {anyType的=的CountryOrRegion印度; }; }; ContactSource = ActiveDirectory的;姓=约瑟。 }; }; }; }; }; }
试试这个我认为它会起作用
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();
}
实际上这是一种已知的格式,如果你知道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));
}
}
我希望这可以帮助您解决问题。