如何在Android中解析KSOAP2响应?

时间:2012-03-29 04:53:19

标签: android web-services ksoap2 android-ksoap2

我在Android中使用KSoap2调用了一个web服务,但我得到的响应如下。如何在Android中解析这个ksoap响应?

  

ResolveNamesResponse {ResponseMessages = anyType的{ResolveNamesResponseMessage = anyType的{的MessageText =多   结果被发现。 ResponseCode = ErrorNameResolutionMultipleResults;   DescriptiveLinkKey = 0;   ResolutionSet = {anyType的分辨率= {anyType的邮箱= anyType的{名称= Amyj;   EmailAddress=Amyj@testsa.onmicrosoft.com; RoutingType = SMTP;   MailboxType =邮箱; }; Contact = anyType {DisplayName = Amy John;   给定名称=艾米;   EmailAddresses = anyType的{条目= SIP:Amyj@test.onmicrosoft.com;   条目= SMTP:Amyj@testsa.onmicrosoft.com; };   PhysicalAddresses = {anyType的输入= {anyType的=的CountryOrRegion中国; }; };   ContactSource = ActiveDirectory的;姓=约翰; }; };   分辨率= {anyType的邮箱= anyType的{名称= Amyraj;   EmailAddress=Amyraj@testsa.onmicrosoft.com; RoutingType = SMTP;   MailboxType =邮箱; }; Contact = anyType {DisplayName = Amy Raj;   给定名称=艾米;   EmailAddresses = anyType的{条目= SIP:Amyraj@testsa.onmicrosoft.com;   条目= SMTP:Amyraj@testsa.onmicrosoft.com; };   PhysicalAddresses = {anyType的输入= {anyType的=的CountryOrRegion印度; }; };   ContactSource = ActiveDirectory的;姓=拉吉; }; };   分辨率= {anyType的邮箱= {anyType的名称=光泽;   EmailAddress=shine@testsa.onmicrosoft.com; RoutingType = SMTP;   MailboxType =邮箱; }; Contact = anyType {DisplayName = Shine Joseph;   给定名称=服务;   EmailAddresses = anyType的{条目= SIP:shine@testsa.onmicrosoft.com;   条目= SMTP:shine@testsa.onmicrosoft.com; };   PhysicalAddresses = {anyType的输入= {anyType的=的CountryOrRegion印度; }; };   ContactSource = ActiveDirectory的;姓=约瑟。 }; }; }; }; }; }

2 个答案:

答案 0 :(得分: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();            
}

答案 1 :(得分:-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));
    } 
}

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