android - 从 Android kSOAP 反序列化 WCF 服务中的参数时出错

标签 android wcf soap deserialization ksoap2

8/5/11:在帖子底部附近更新

我正在尝试使用 kSOAP 从我的 Android 应用程序调用我的 WCF 网络服务方法。该方法有 4 个参数。其中 3 个属于 Guid 类型(编码 Java 的 UUID 对其起作用),最后一个是自定义类型:LocationLibrary.Location。这种类型位于我加载到 WCF Web 服务项目中的单独 DLL (LocationLibrary) 中,它基本上由两个 double 组成,纬度和经度。

    [OperationContract]       
    byte[] GetData(Guid deviceId, Guid appId, Guid devKey, LocationLibrary.Location loc);

项目LocationLibrary中的Location类很简单:

    namespace LocationLibrary
    {
    public class Location
    {
        public double latitude { get; set; }

        public double longitude { get; set; }

        public new string ToString()
        {
            return latitude.ToString() + "," + longitude.ToString();
        }

        public bool Equals(Location loc)
        {
            return this.latitude == loc.latitude && this.longitude == loc.longitude;
        }
    }
}

在我的 Android 项目中,我创建了一个类似于 .NET 版本的名为“Location”的类:

    public class Location {
    public double latitude;
    public double longitude;

    public Location() {}

    public static Location fromString(String s)
    {
    //Format from SOAP message is "anyType{latitude=39.6572799682617; longitude=-78.9278602600098; }"
    Location result = new Location();
    String[] tokens = s.split("=");
    String lat = tokens[1].split(";")[0];
    String lng = tokens[2].split(";")[0];
    result.latitude = Double.parseDouble(lat);
    result.longitude = Double.parseDouble(lng); 
    return result;
    }

    public String toString()
    {       
    return Double.toString(latitude) + "," + Double.toString(longitude);    
    }
}

当使用 kSOAP 连接到网络服务时,我执行以下操作:

    private final String SOAP_ACTION = "http://tempuri.org/IMagicSauceV3/GetData";
    private final String OPERATION_NAME = "GetData";     
    private final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    private final String SOAP_ADDRESS = "http://mydomain.com/myservice.svc";
    private final SoapSerializationEnvelope envelope;

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
    request.addProperty(Cloud8Connector.deviceIdProperty);
    request.addProperty(Cloud8Connector.appIdProperty);
    request.addProperty(Cloud8Connector.devKeyProperty);        
    PropertyInfo locationProperty = new PropertyInfo();
    locationProperty.setName("loc");
    locationProperty.setValue(Cloud8Connector.myLoc);
    locationProperty.setType(Location.class);
    request.addProperty(locationProperty);

    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
       envelope.setOutputSoapObject(request);
       MarshalUUID mu = new MarshalUUID();
    mu.register(envelope);
    MarshalLocation ml = new MarshalLocation();
    ml.register(envelope);

     byte[] result = null;
    HttpTransportSE httpRequest = new HttpTransportSE(SOAP_ADDRESS);

    try
    {
        httpRequest.call(SOAP_ACTION, envelope);

        String payloadString = ((SoapPrimitive)(envelope.getResponse())).toString();
        result = Base64.decode(payloadString, Base64.DEFAULT);         
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

如您所见,我为 Location 创建了一个简单的 Marshal 类,它基本上只使用 fromString 和 toString 方法。我使用位置的编码(marshal)实例注册信封:

    MarshalLocation ml = new MarshalLocation();
    ml.register(envelope);

这是 Location 的 marshal 类:

    public class MarshalLocation implements Marshal
    {

    public Object readInstance(XmlPullParser parser, String namespace, String name, 
            PropertyInfo expected) throws IOException, XmlPullParserException {
        return Location.fromString(parser.nextText());                                
    }

    public void register(SoapSerializationEnvelope cm) {
         cm.addMapping(cm.xsd, "Location", Location.class, this);

    }

    public void writeInstance(XmlSerializer writer, Object obj) throws IOException {

            writer.text(((Location)obj).toString());             
        }    
    }

但是,我从 WCF 返回了这个错误,似乎无法让它工作。从错误和搜索中,我认为我需要对我的网络服务进行一些调整,但我不确定解决这个问题的最佳方法到底是什么。

我已经尝试将其添加到 Web 服务的 web.config 文件中:

        <system.runtime.serialization>
    <dataContractSerializer>
        <declaredTypes>
            <add type="Location, LocationLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null">
                <knownType type="Location, LocationLibrary, Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"></knownType>
            </add>
        </declaredTypes>
    </dataContractSerializer>
</system.runtime.serialization>

我已经尝试将 ServiceKnownType 属性添加到接口(interface)方法签名中:

    [OperationContract]       
    [ServiceKnownType(typeof(LocationLibrary.Location))]
    byte[] GetData(Guid deviceId, Guid appId, Guid devKey, LocationLibrary.Location loc);

但我仍然得到这个错误:(

你能给我指出正确的方向吗?谢谢!

错误:

07-30 00:42:08.186: WARN/System.err(8723): SoapFault - faultcode: 'a:DeserializationFailed' faultstring: 'The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:loc. The InnerException message was 'Error in line 1 position 522. Element 'http://tempuri.org/:loc' contains data from a type that maps to the name 'http://www.w3.org/2001/XMLSchema:Location'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'Location' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.' faultactor: 'null' detail: org.kxml2.kdom.Node@4053aa28

更新

理想情况下,我不必将 LocationLibrary 类移动到主 WCF 项目中,因为这会使处理希望它位于不同命名空间中的遗留客户端变得复杂。但是,我确实通过将这些类移动到主要 WCF 项目中并将 java 中的 marshal 类修改为:

    public void register(SoapSerializationEnvelope cm) {            
         cm.addMapping("http://schemas.datacontract.org/2004/07/MagicSauceV3", "Location", Location.class, this);
    }        

    public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
            Location loc = (Location)obj;
            writer.startTag("http://schemas.datacontract.org/2004/07/MagicSauceV3", "latitude");
            writer.text(Double.toString(loc.latitude));
            writer.endTag("http://schemas.datacontract.org/2004/07/MagicSauceV3", "latitude");
            writer.startTag("http://schemas.datacontract.org/2004/07/MagicSauceV3", "longitude");
            writer.text(Double.toString(loc.longitude));
            writer.endTag("http://schemas.datacontract.org/2004/07/MagicSauceV3", "longitude");       
        }

既然我让它工作了,我就让它与 LocationLibrary WCF 类一起工作(在单独的 DLL 中)。所以我修改了 marshal 类:

    public void register(SoapSerializationEnvelope cm) {            
         cm.addMapping("http://schemas.datacontract.org/2004/07/LocationLibrary", "Location", Location.class, this);
    }


    public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
            Location loc = (Location)obj;
            writer.startTag("http://schemas.datacontract.org/2004/07/LocationLibrary", "latitude");
            writer.text(Double.toString(loc.latitude));
            writer.endTag("http://schemas.datacontract.org/2004/07/LocationLibrary", "latitude");
            writer.startTag("http://schemas.datacontract.org/2004/07/LocationLibrary", "longitude");
            writer.text(Double.toString(loc.longitude));
            writer.endTag("http://schemas.datacontract.org/2004/07/LocationLibrary", "longitude");       
        }

这似乎应该会成功,因为它生成的 XML 与确实有效的 WP7 版本相似。

WP7 工作 XML 请求:

<GetData xmlns="http://tempuri.org/">
<deviceId>{valid GUID}</deviceId>
<appId>{valid GUID}</appId>
<devKey>{valid GUID}</devKey>
<loc xmlns:d4p1="http://schemas.datacontract.org/2004/07/LocationLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:latitude>47.65</d4p1:latitude>
<d4p1:longitude>-122.34</d4p1:longitude>
</loc>
</GetData>

Android 非工作 XML 请求:

<GetData xmlns="http://tempuri.org/" id="o0" c:root="1">
<deviceId i:type="d:UUID">{valid GUID}</deviceId>
<appId i:type="d:UUID">{valid GUID}</appId>
<devKey i:type="d:UUID">{valid GUID}</devKey>
<loc i:type="n0:Location" xmlns:n0="http://schemas.datacontract.org/2004/07/LocationLibrary">
<n0:latitude>47.65</n0:latitude>
<n0:longitude>-122.34</n0:longitude>
</loc>
</GetData>

上面的 Android XML 请求产生了这个响应错误:

08-05 22:51:23.703: WARN/System.err(1382): SoapFault - faultcode: 'a:DeserializationFailed' faultstring: '格式化程序在尝试反序列化消息时抛出异常:试图反序列化参数 http://tempuri.org/:loc . InnerException 消息是“第 1 行位置 590 中的错误。元素“http://tempuri.org/:loc”包含来自映射到名称“http://schemas.datacontract.org/2004/07/”的类型的数据位置库:位置'。反序列化器不知道映射到该名称的任何类型。考虑使用 DataContractResolver 或将与“位置”对应的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给 DataContractSerializer 的已知类型列表中。有关详细信息,请参阅 InnerException。 faultactor:'null' 详细信息:org.kxml2.kdom.Node@4054bcb8

我看到的唯一显着区别是工作 XML 请求中的“loc”属性: xmlns:i="http://www.w3.org/2001/XMLSchema-instance"

我不知道这是否会有所不同(而且它似乎没有被嵌套标签引用)。我也不知道如何在不弄乱另一个 namespace 的情况下添加额外的 namespace 。

抱歉发了这么长的帖子,但我想确保你拥有所有需要帮助的信息:)

谢谢!

最佳答案

如我所料,您只是在做 ToString 但这是错误的。您的位置不像两个串联值的字符串那样传输。它作为序列化为 XML 的对象进行传输。像这样的东西:

<Location> <!-- or loc -->
   <latitude>10.0</latitude>
   <longitude>-20.0</longitude>
</Location>

关于android - 从 Android kSOAP 反序列化 WCF 服务中的参数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907053/

相关文章:

android - 内部 SD 卡和内部存储之间的区别。

c# - WCF IList 序列化问题

javax.xml.ws.WebServiceException : Could not find service named

java - 使用 Java 在 XML 中设置嵌入字段的文本

c# - 即使正确发送了 SOAP 请求,Axis2 也总是收到空参数?

android - BroadcastReceiver 无法与警报管理器一起使用

android - Android 中的 Unicode 问题

Java Picasa API 目录列表不起作用

wcf - 生成的元数据 (WSDL) 中的 .net 4 WCF 端点指向节点而不是托管在负载平衡 (NLBS) IIS6 上的虚拟主机

session - 带有流和 session 的 NetTcpBinding