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

kSOAP - Request property with attribute and namespaced value

我正在尝试从 Android 调用 SOAP .NET 网络服务,它需要一个包含如下元素(从 .NET 网络服务客户端生成)的请求:

<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 为 Android 生成这种类型的 SOAP 属性?

我不太确定这是否是解决您问题的灵丹妙药,但 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 会喜欢这个 ;) 此致,马尔辛