2015-05-26 112 views
3

使用KSOAP爲accesing從Android的一個WSDL者地位解析SOAP響應現在用的是以下沒有安卓

HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(BASIC_URL); 
    try { 
     StringEntity se = new StringEntity(SoapRequest, HTTP.UTF_8); 
     se.setChunked(true); 

     se.setContentType("text/xml"); 
     httpPost.addHeader("Accept-Encoding", "gzip,deflate"); 
     httpPost.addHeader("SOAPAction", SoapAction); 
     httpPost.addHeader("Content-Type", "text/xml;charset=UTF-8"); 
     httpPost.addHeader(header); 
     httpPost.setEntity(se); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity resEntity = httpResponse.getEntity(); 
     // response = EntityUtils.toString(resEntity); 
     return httpResponse; 
    } catch (Exception e) { 

    } 

其中SoapRequest是肥皂串,得到了來自服務器的響應,但如何解析,因爲上午SOAP響應不使用HttpTransportSEksoap我沒有肥皂對象作爲響應。

  1. 這是從android訪問wsdl servise的正確方法嗎?
  2. 我可以皁對象轉換爲XML或JSON,然後解析它

樣本響應是

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org /soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
    <GetResponse xmlns="http://tempuri.org/"> 
    <GetResult> 
     <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> 
       <xs:complexType> 
       <xs:choice minOccurs="0" maxOccurs="unbounded"> 
        <xs:element name="First"> 
         <xs:complexType> 
          <xs:sequence> 
          <xs:element name="FirstElement" type="xs:int" minOccurs="0"/> 
          </xs:sequence> 
         </xs:complexType> 
        </xs:element> 

       </xs:choice> 
       </xs:complexType> 
      </xs:element> 
     </xs:schema> 
      <NewDataSet xmlns=""> 
       <Exception diffgr:id="Exception1" msdata:rowOrder="0"> 
       <ex_id>12</ex_id> 
       </Exception>    
       <Second diffgr:id="Second" msdata:rowOrder="0"> 
       <SecondElement>66</SecondElement> 
       </Second> 
      </NewDataSet> 
    </GetResult> 
    </GetResponse> 

回答

0

我們可以解析SOAP響應作爲解析xml.Using xmlpull解析器我們可以提取xml tags。認爲他們不需要HttpTransportSEksoap

public void fetchXML(){ 
    Thread thread = new Thread(new Runnable(){ 
     @Override 
     public void run() { 
      try { 

       XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance(); 
       XmlPullParser myparser = xmlFactoryObject.newPullParser(); 

       myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES 
         , false); 
       myparser.setInput(new StringReader("Soap response here")); 
       parseXML(myparser); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 

    thread.start(); 
} 

public void parseXML(XmlPullParser myParser) { 
    int event; 
    String text=null; 
    try { 
     event = myParser.getEventType(); 
     while (event != XmlPullParser.END_DOCUMENT) { 
      String name=myParser.getName(); 
      switch (event){ 
       case XmlPullParser.START_TAG: 
        break; 
       case XmlPullParser.TEXT: 
        text = myParser.getText(); 
        break; 

       case XmlPullParser.END_TAG: 
        if(name.equals("ex_id")){ 
         Log.i("----", text); 
        } 

        break; 
      } 
      event = myParser.next(); 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

}