Android的ksoap2可空类型 [英] Android ksoap2 nullable type

查看:222
本文介绍了Android的ksoap2可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从使用Ksoap2库Android设备连接到一个WCF .NET Web服务。一切正常,我已经能够发送和接收复杂的对象为止(排除了很多之后)。不过,我现在遇到了可空类型的问题。在服务器端,有很多我会送的属性将是空。当我试图把这些作为空从Android身边我得到一个反序列化的错误,因为KSOAP提出,而不是无空=真=真。下面是一个测试车手的一些工作SOAP XML以及来自Android客户端当前XML。

I am trying to connect to a WCF .Net Web Service from an Android device using the Ksoap2 library. Everything works fine, I've been able to send and receive complex objects so far (after a LOT of troubleshooting). However, I'm now running into the problem of nullable types. On the server-side, a lot of the attributes I'll be sending will be nullable. When I try to send these as null from the Android side I get a deserialization error because ksoap puts null=true instead of nil=true. Here is some working SOAP XML from a test driver as well as the current XML from the Android client.

工作试车手XML

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <AddNullables xmlns="http://TJIsGhey/Tester">
        <NumbersToAdd xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Input1>7</Input1>
            <Input2 i:nil="true" />
        </NumbersToAdd>
    </AddNullables>
</s:Body>
</s:Envelope>

Android客户端的XML

Android Client 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 />
    <v:Body>
        <AddNullables xmlns="http://TJIsGhey/Tester" id="o0" c:root="1">
            <NumbersToAdd i:type="n0:NullablesIn" xmlns:n0="http://TJIsGhey/Tester">
                <Input1 i:type="d:int">6</Input1>
                <Input2 i:null="true" />
            </NumbersToAdd>
        </AddNullables>
    </v:Body>
</v:Envelope>

下面是错误消息我收到:

And here is the error message I'm receiving:

有一个错误反序列化类型Tester.NullablesIn的对象。值''不能被解析为类型的Int32。

There was an error deserializing the object of type Tester.NullablesIn. The value '' cannot be parsed as the type 'Int32'.

任何帮助将是很大的AP preciated!

Any help would be greatly appreciated!

推荐答案

我有同样的问题。 当我钻出内Ksoap2源$ C ​​$ C,我发现这个问题。 你有2可能的解决方案,第一和容易的将是使用VER12在SoapSerializationEnvelope。 因为Ksoap2仅使用自VER12和向上nil属性。 然而,你可能会被这种变化影响,所以你有另一种选择: 继承了SoapSerializationEnvelope并重写这个类的写属性的方法。 像这样:

I had the same issue. After I drilled inside Ksoap2 source code I found the issue. You have 2 possible solutions the first and easy one would be to use VER12 at the SoapSerializationEnvelope. Since Ksoap2 is only using nil attribute since VER12 and up. However you might be effected by this change so you have another option: Inherit the SoapSerializationEnvelope and override the writeProperty method of this class. as so:

public class ExtendedSoapSerializationEnvelope extends
    SoapSerializationEnvelope {

public ExtendedSoapSerializationEnvelope(int version) {
    super(version);
}

@Override
protected void writeProperty(XmlSerializer writer, Object obj,
        PropertyInfo type) throws IOException {
    if (obj == null) {

        if (!(obj instanceof SoapObject)) {
            // assuming object implements KvmSerializable or other type of
            // Serialization interface
            writer.attribute(xsi, version >= VER11 ? "nil" : "null", "true");

        } else {
            // assuming SoapObject being used with VER12 and up
            writer.attribute(xsi, version >= VER12 ? "nil" : "null", "true");
        }
        return;
    }
    super.writeProperty(writer, obj, type);
}}

如果你知道你的web服务始终与零,你可以跳过此检查工作的尾部添加,只是使用:

If you know that your webService always work with nil you could skip this check altogther and just use:

    @Override
protected void writeProperty(XmlSerializer writer, Object obj,
        PropertyInfo type) throws IOException {
    if (obj == null) {
        writer.attribute(xsi, "nil", "true");
        return;
    }

    super.writeProperty(writer, obj, type);

}

这篇关于Android的ksoap2可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆