如何使用KSOAP在Java中的字符串数组中序列化xml-object?

问题描述:

我有下面的代码从服务器(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”。我可以在哪里犯错?

使用getResponse而不是bodyIn。你会得到一个SoapObject,然后你可以解析你的pojo。