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

时间:2011-10-11 05:43:46

标签: java soap ksoap2

我有来自服务器(WSDL SOAP)的响应,其代码如下:

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”。我哪里可以搞错?

1 个答案:

答案 0 :(得分:0)

使用getResponse而不是bodyIn。您将获得一个SoapObject,然后您可以将其解析为您的pojo。