kSOAP - 具有属性和命名空间值的Request属性

时间:2015-01-16 22:03:12

标签: android soap ksoap2 ksoap

我正在尝试从Android调用SOAP .NET Web服务,该服务需要包含这样的元素的请求(从.NET Web服务客户端生成):

<InputVariable>
    <Id>MyVariable</Id>
    <Value i:type="a:string" xmlns:a="http://www.w3.org/2001/XMLSchema">Hello</Value>
</InputVariable>

这里有一些奇怪的事情,kSOAP似乎并不直接支持。一,是我在kSOAP中找不到一种方法来生成一个也有属性的属性。我发现this answer能够让我更进一步,但仍然不准确。以下是我得到的解决方案:

<InputVariable>
    <Id>MyVariable</Id>
    <Value i:type="http://www.w3.org/2001/XMLSchema:string">Hello</Value>
</InputVariable>

SoapObject inputVariable = new SoapObject("", "InputVariable");
inputVariable.addProperty("Id", "MyVariable");

AttributeInfo att = new AttributeInfo();
att.setName("type");
att.setNamespace("http://www.w3.org/2001/XMLSchema-instance");
// I need some way to set the value of this to a namespaced string
att.setValue("http://www.w3.org/2001/XMLSchema:string");

ValueSoapObject valueProperty = new ValueSoapObject("", "Value");
valueProperty.setText("Hello");
valueProperty.addAttribute(att);
inputVariable.addSoapObject(valueProperty);

在运行时,服务器失败并显示无法反序列化的错误:     Value' contains data from a type that maps to the name '://www.w3.org/2001/XMLSchema:string'. The deserializer has no knowledge of any type that maps to this name.

如何使用kSOAP for Android生成此类型的SOAP属性?

1 个答案:

答案 0 :(得分:1)

我不确定这是否是您的问题的灵丹妙药,但是SoapSerializationEnvelope :: implicitTypes设置为false,强制向值添加类型。太好了。你的请求的简单版本如下:

SoapObject request = new SoapObject("", "InputVariable");
request.addProperty("Id", "MyVariable");
request.addProperty("Value", "Hello");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.implicitTypes = false;
envelope.setOutputSoapObject(request);

产生这样的请求:

<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 />
    <v:Body>
        <InputVariable xmlns="" id="o0" c:root="1">
            <Id i:type="d:string">MyVariable</Id>
            <Value i:type="d:string">Hello</Value>
        </InputVariable>
    </v:Body>
</v:Envelope>

可能你的WS会喜欢这个;) 此致,Marcin