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



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

所以我看了另一篇 SO 帖子:序列化一个整数数组以使用 kSOAP2 发送但是我遇到了与该帖子的作者相同的问题-我收到一个错误,说:

格式化程序在尝试反序列化 消息:尝试反序列化参数时出错 http://tempuri.org/:recipientIds。内部异常消息是 '第 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>

所以我的猜测是问题出在收件人 Ids 命名空间上......在 iOS 中我设置的地方,我目前只有安卓,但我不确定如何使用我的 SoapObject 正确设置它......

尝试像这样添加收件人

request.addProperty("Arrays", recipients);

甚至

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

最新更新