2012-11-27 91 views
-1

我接觸到在線web服務,並能夠得到迴應。我需要使用kso​​ap2解析這個響應。我試着用SAX解析器,我能夠解析it.please幫助我如何解析使用kso​​ap2在android中使用kso​​ap2解析肥皂響應?

的XML這是我的XML響應

<string xmlns="http://www.webserviceX.NET/"> 
<StockQuotes> 
<Stock> 
<Symbol>M</Symbol> 
<Last>39.86</Last> 
<Date>11/26/2012</Date> 
<Time>4:02pm</Time> 
<Change>-1.87</Change> 
<Open>41.00</Open> 
<High>41.09</High> 
<Low>39.65</Low> 
<Volume>7968614</Volume> 
<MktCap>15.765B</MktCap> 
<PreviousClose>41.73</PreviousClose> 
<PercentageChange>-4.48%</PercentageChange> 
<AnnRange>30.38 - 42.17</AnnRange> 
<Earns>3.227</Earns> 
<P-E>12.93</P-E> 
<Name>Macy's Inc Common</Name> 
</Stock> 
</StockQuotes> 
</string> 
+0

https://www.youtube.com/watch ?V = SN-GNPGDPc8。谷歌它,你會發現很多的例子。至少發佈你迄今爲止所做的事情。粘貼解析代碼。 – Raghunandan

+0

只顯示響應是不夠的。您還應該詳細說明如何解析它。 – Gem

回答

1

我貼你我使用的示例代碼使用.NET webservices解析Ksoap2 的結果。我希望它有幫助。

與數據類型,像這樣創建一個類:

import org.ksoap2.serialization.KvmSerializable; 
import org.ksoap2.serialization.PropertyInfo; 

public class Provincias implements KvmSerializable { 

    public int GCPRV; 
    public String GDPRV; 
    public int GFCTB; 

    public Provincias(){} 
    public Provincias(int gcprv, String gdprv, int gfctb) 
    { 
     this.GCPRV=gcprv; 
     this.GDPRV=gdprv; 
     this.GFCTB=gfctb;  
    } 

    @Override 
    public Object getProperty(int arg0) { 
      switch(arg0) 
      { 
      case 0: 
       return GCPRV; 
      case 1: 
       return GDPRV; 
      case 2: 
       return GFCTB; 
      } 

      return null; 
    } 

    @Override 
    public int getPropertyCount() { 
     return 3; 
    } 

    @Override 
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { 
      switch(index) 
      { 
      case 0: 
       info.type = PropertyInfo.INTEGER_CLASS; 
       info.name = "GCPRV"; 
       break; 
      case 1: 
       info.type = PropertyInfo.STRING_CLASS; 
       info.name = "GDPRV"; 
       break; 
      case 2: 
       info.type = PropertyInfo.INTEGER_CLASS; 
       info.name = "GFCTB"; 
       break; 
      default:break; 
      } 

    } 

一旦你有你SoapObject響應,你只需要查詢它的propertis 並將其綁定到類像這樣:

Provincias[] prov = new Provincias[ObjetoSoap.getPropertyCount()]; 
       for (int i = 0; i < prov.length; i++) { 
        SoapObject pii = (SoapObject)ObjetoSoap.getProperty(i); 
        Provincias provincia = new Provincias();  
        provincia.GCPRV=Integer.parseInt(pii.getProperty(0).toString()); 
        provincia.GDPRV =pii.getProperty(1).toString(); 
        provincia.GFCTB=Integer.parseInt(pii.getProperty(2).toString());     
        prov[i] = provincia; 
      } 

編輯:在這裏你有方法說明:

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <Cargar_Provincias_JavaResponse xmlns="http://tempuri.org/"> 
     <Cargar_Provincias_JavaResult> 
     <Provincias_Class> 
      <GCPRV>int</GCPRV> 
      <GDPRV>string</GDPRV> 
      <GFCTB>int</GFCTB> 
     </Provincias_Class> 
     <Provincias_Class> 
      <GCPRV>int</GCPRV> 
      <GDPRV>string</GDPRV> 
      <GFCTB>int</GFCTB> 
     </Provincias_Class> 
     </Cargar_Provincias_JavaResult> 
    </Cargar_Provincias_JavaResponse> 
    </soap:Body> 
</soap:Envelope> 
+0

您可以爲上述xml響應提供代碼\ – Kumar

+0

我粘貼web服務的WSDL描述,以便您可以檢查它 –