【问题标题】:kSOAP - Request property with attribute and namespaced valuekSOAP - 请求具有属性和命名空间值的属性
【发布时间】:2015-01-16 22:03:12
【问题描述】:

我正在尝试从 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 for Android 生成这种类型的 SOAP 属性?

【问题讨论】:

    标签: android soap ksoap2 ksoap


    【解决方案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 可能会喜欢这个 ;) 问候,马尔辛

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2011-03-05
    • 1970-01-01
    • 2020-10-03
    相关资源
    最近更新 更多