使用kSOAP2发送带有int数组的对象

时间:2012-05-31 12:37:32

标签: android arrays soap ksoap2

之前我用kSOAP 2发送了几个简单对象,但现在我必须发送一个包含整数数组的对象...

所以我查看了其他SO帖子:Serialize an array of ints to send using kSOAP2 但是我遇到了与该帖子的作者相同的问题 - 我得到一个错误回复说:

  

格式化程序在尝试反序列化时抛出异常   消息:尝试反序列化参数时出错   http://tempuri.org/:recipientIds。 InnerException消息是   '第1行位置650的错误。元素   “http://tempuri.org/:recipientIds”包含来自映射类型的数据   这个名字   'http://schemas.microsoft.com/2003/10/Serialization/Arrays:recipientIds'。   反序列化器不知道映射到此名称的任何类型。

我的课程:

public class AddMessageConnection {
    private static final String SOAP_ACTION = "http://tempuri.org/IKindergarden/AddMessageAboutKid";
    private static final String METHOD_NAME = "AddMessageAboutKid";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String NESTED_NAMESPACE = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
    private String URL;
    public String msg;
    public String kidID;
    public ArrayList<String> listOfEmployees = new ArrayList<String>();

    void connection()
    {
        Singleton service = Singleton.getInstance();
        service.getURL();
        String firstURL = service.getURL();
        URL = firstURL + "Kindergarden.svc";

        //Get parentID, parentToken
        String parentIDSingleton = service.getParentID();
        String parentTokenSingleton = service.getParentToken();

        //Initialize soap request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        //Add parameters
        request.addProperty("authorId", service.getParentID());
        request.addProperty("relatesToKidId", kidID);

        SoapObject recipients = new SoapObject(NESTED_NAMESPACE, "recipientIds");

        //Add the recipients
        List<Integer> recp = new ArrayList<Integer>();
        //Iterate through recipients and add them
        for(int i = 0; i < listOfEmployees.size(); i++)
        {
            int converted = Integer.parseInt(listOfEmployees.get(i));
            recp.add(converted);
        }

        for(Integer i:recp)
        {
            recipients.addProperty("int", i);   
        }

        request.addSoapObject(recipients);

        request.addProperty("sMessage", msg);

        //Declare the version of the SOAP request
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        //Debugging
        envelope.avoidExceptionForUnknownProperty=true;

        envelope.dotNet=true;
        envelope.implicitTypes=true;
        envelope.setAddAdornments(false);

        //Prepare request
        envelope.setOutputSoapObject(request);

        //Needed to make the internet call
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        //Allow for debugging - needed to output the request
        androidHttpTransport.debug = true;

        try 
        {
            //this is the actual part that will call the web service
            androidHttpTransport.call(SOAP_ACTION, envelope);

            System.out.println("Request: " + androidHttpTransport.requestDump.toString());
            System.out.println("Response: " + androidHttpTransport.responseDump.toString());

        } catch(org.xmlpull.v1.XmlPullParserException ex2)
        {               
            System.out.println(androidHttpTransport.requestDump.toString());

        }  catch (Exception e)
        {
            e.printStackTrace();
            System.out.println(androidHttpTransport.requestDump.toString());
        }
    }
}

所以我试图通过假设错误在于命名空间来解决这个问题...... 有没有人知道我应该改变什么?

SoapUI说服务期望:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:AddMessageAboutKid>
         <!--Optional:-->
         <tem:authorId>?</tem:authorId>
         <!--Optional:-->
         <tem:relatesToKidId>?</tem:relatesToKidId>
         <!--Optional:-->
         <tem:recipientIds>
            <!--Zero or more repetitions:-->
            <arr:int>?</arr:int>
         </tem:recipientIds>
         <!--Optional:-->
         <tem:sMessage>?</tem:sMessage>
      </tem:AddMessageAboutKid>
   </soapenv:Body>
</soapenv:Envelope>

我在iOS中完成了请求,其中xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:day="http://schemas.datacontract.org/2004/07/DayCare.DB.DTO">
<s:Header>
    <parentId xmlns="ns">2104541932</parentId>
    <parentToken xmlns="ns">00430050004A00430046004C0052</parentToken>
    <authType xmlns="ns">Parent</authType>
    <id xmlns="ns">1</id>
</s:Header>
<s:Body>
<AddMessageAboutKid xmlns="http://tempuri.org/">
    <authorId>2104541932</authorId>
    <relatesToKidId>2104535510</relatesToKidId>
    <recipientIds xmlns:d4p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <d4p1:int>2104535836</d4p1:int>
        <d4p1:int>2104535839</d4p1:int>
    </recipientIds>
    <sMessage>Test</sMessage>
</AddMessageAboutKid>
</s:Body>
</s:Envelope>

而来自kSOAP2的我的xml最终为:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header>
    <n0:parentId xmlns:n0="ns">2104536774</n0:parentId>
    <n1:parentToken xmlns:n1="ns">0003</n1:parentToken>
    <n2:authType xmlns:n2="ns">Parent</n2:authType>
    <n3:id xmlns:n3="ns">1</n3:id>
</v:Header>
<v:Body>
<AddMessageAboutKid xmlns="http://tempuri.org/">
    <authorId>2104536774</authorId>
    <relatesToKidId>2104535421</relatesToKidId>
    <recipientIds i:type="n4:recipientIds" xmlns:n4="http://tempuri.org/">
        <int>2104535482</int>
        <int>2104535485</int>
    </recipientIds>
    <sMessage>Test</sMessage>
</AddMessageAboutKid>
</v:Body>
</v:Envelope>

所以我的猜测是问题在于recipientIds命名空间...在我设置的iOS中,我目前只有Android,但我不确定如何使用我的SoapObjects正确设置... < / p>

1 个答案:

答案 0 :(得分:0)

尝试添加此类收件人

request.addProperty("Arrays", recipients);

甚至

request.addProperty("http://schemas.microsoft.com/2003/10/Serialization/Arrays", recipients);