2011-10-11 42 views
0

我有下面的代碼從服務器(WSDL SOAP)響應:如何使用KSOAP在Java中的字符串數組中序列化xml-object?

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> 
    <GetChanelResponse xmlns="http://tempuri.org/"> 
     <GetChanelResult> 
     <string>string</string> 
     <string>string</string> 
     </GetChanelResult> 
    </GetChanelResponse> 
    </soap:Body> 
</soap:Envelope> 

我嘗試使用此代碼用於獲取字符串數組:

import java.util.ArrayList; 
import java.util.Hashtable; 
import java.util.Vector; 

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


public class XMLRez extends Vector implements KvmSerializable{ 

    private static final long serialVersionUID = -1166006770093411055L; 

    @Override 
    public Object getProperty(int arg0) { 
     // TODO Auto-generated method stub 
     return this.get(arg0); 
    } 

    @Override 
    public int getPropertyCount() { 
     // TODO Auto-generated method stub 
     return this.size(); 

    } 

    @Override 
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) { 
     // TODO Auto-generated method stub 
     arg2.name = "string"; 
     arg2.type = PropertyInfo.STRING_CLASS; 

    } 

    @Override 
    public void setProperty(int arg0, Object arg1) { 
     // TODO Auto-generated method stub 
     this.add(arg1.toString()); 
    } 
} 

該類用於數據存儲。

public class Main { 

    public static String SOAP_ACTION="http://tempuri.org/GetChanel"; 
    public static String METHOD_NAME="GetChanel"; 
    public static String NAMESPACE="http://tempuri.org/"; 
    public static String URL="http://www.mcds.co.il/YouTube/ChanelApi.asmx"; 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     XMLRez documentIdVector=new XMLRez(); 


     PropertyInfo documentIdsPropertyInfo = new PropertyInfo(); 
     documentIdsPropertyInfo.setName("GetChanelResult"); 
     documentIdsPropertyInfo.setValue(documentIdVector); 
     documentIdsPropertyInfo.setType(documentIdVector.getClass()); 

     // TODO Auto-generated method stub 
     SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME); 
     request.addProperty(documentIdsPropertyInfo); 
     //request.addProperty("Celsius", "5"); 

     SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     envelope.dotNet=true;  
     envelope.setOutputSoapObject(request); 
     envelope.addMapping(NAMESPACE, "GetChanelResult", new XMLRez().getClass()); 

     //AndroidHttpTransport aht=new AndroidHttpTransport(URL); 
     HttpTransportSE aht =new HttpTransportSE(URL); 

     try { 
      aht.call(SOAP_ACTION, envelope); 
      XMLRez prim=(XMLRez)envelope.bodyIn; 
     } catch (Exception e) { 
      //Log.e("exp", e.getMessage()); 
      System.out.println(e.getMessage()); 
     } 
    } 

} 

這個類用於從服務中獲取數據。但我總是得到異常「org.ksoap2.serialization.SoapObject不能轉換爲XMLRez」。我可以在哪裏犯錯?

回答

0

使用getResponse而不是bodyIn。你會得到一個SoapObject,然後你可以解析你的pojo。

相關問題