KSOAP2具有数组响应解析

时间:2014-04-22 19:06:31

标签: android ksoap2

解析ksoap响应时遇到问题。

请求转储:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:server" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <tns:get_event>
         <phone xsi:type="xsd:string">7827701616</phone>
      </tns:get_event>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

响应转储:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:server" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:get_eventResponse xmlns:ns1="urn:server">
         <return xsi:type="tns:events_data">
            <events xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:events[3]">
               <item xsi:type="tns:events">
                  <groom xsi:type="xsd:string">sanskaar</groom>
                  <bride xsi:type="xsd:string">saumya</bride>
                  <event_name xsi:type="xsd:string">wedding</event_name>
                  <venue xsi:type="xsd:string">New Delhi</venue>
                  <event_date xsi:type="xsd:string">Tuesday April 14, 2014</event_date>
               </item>
               <item xsi:type="tns:events">
                  <groom xsi:type="xsd:string">sanskaar</groom>
                  <bride xsi:type="xsd:string">saumya</bride>
                  <event_name xsi:type="xsd:string">hzbrgbj</event_name>
                  <venue xsi:type="xsd:string">New Delhi</venue>
                  <event_date xsi:type="xsd:string">Tuesday April 14, 2014</event_date>
               </item>
               <item xsi:type="tns:events">
                  <groom xsi:type="xsd:string">sanskaar</groom>
                  <bride xsi:type="xsd:string">saumya</bride>
                  <event_name xsi:type="xsd:string">wedding</event_name>
                  <venue xsi:type="xsd:string">New Delhi</venue>
                  <event_date xsi:type="xsd:string">Tuesday April 14, 2014</event_date>
               </item>
            </events>
         </return>
      </ns1:get_eventResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

解析响应的活动代码:

ht.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
System.out.print(response);//getting output here checked by debugging

for(int i=0;i<response.getPropertyCount();i++){    
       Object property = response.getProperty(i);
       if(property instanceof SoapObject)//returning false
       {
           SoapObject final_object = (SoapObject) property;
           String groom = final_object.getProperty("groom").toString();
    }
}

我收到了KSOAP回复的回复,但我无法理解为什么我无法解析回复。

由于If返回false,代码执行时没有错误但未进入property instanceof SoapObject条件。

这是为什么?请指导我

2 个答案:

答案 0 :(得分:2)

我有同样的问题,我使用Vector来解决它:

解析代码:

ht.call(SOAP_ACTION, envelope);

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

for(int i=0;i<response.getPropertyCount();i++){    

       Vector resV = (Vector)response.getProperty(i); 
       int resVlenght = resV.size();

       for(int count = 0;count<resVlenght;count++)
       {
           HashMap<String,String> value = new HashMap<String,String>();
           String n = resV.elementAt(count).toString();
           SoapObject p = (SoapObject)resV.elementAt(count);
           System.out.print(n);

           String groom= p.getProperty("groom").toString();

       }

}

答案 1 :(得分:-1)

Object property = (SoapObject) response.getProperty(i);

尝试这样做