使用kso​​ap2从android传递对象到wcf soap服务;它发送和接收0

使用kso​​ap2从android传递对象到wcf soap服务;它发送和接收0,第1张

概述我试图使用kso​​ap2来访问来自android的wcfsoap服务中的方法.我能够成功地将简单类型参数传递给方法,它工作正常.但是当我尝试传递对象时,wcf方法对所有对象值都接收0.我在WP7应用程序中使用了相同的SOAP服务,运行正常.我的服务中有2个方法,ksoapadd和addParam.addParam接受

我试图使用kso​​ap2来访问来自android的wcf soap服务中的方法.
我能够成功地将简单类型参数传递给方法,它工作正常.
但是当我尝试传递对象时,wcf方法对所有对象值都接收0.

我在WP7应用程序中使用了相同的SOAP服务,运行正常.

我的服务中有2个方法,ksoapadd和addParam. addParam接受两个整数并返回它们的总和(这是有效的). ksoapadd接受类testadd的对象,它有两个整数元素,ksoapadd返回它们的总和(这得到并发送0).

我认为这与命名空间有关,但我无法弄清楚如何解决它.我一直在做试验和错误,以获得正确的肥皂请求,但没有运气.

任何人都可以帮我弄清楚该怎么办?我尝试了很多选项,以某种方式帮助其他有类似问题的人,但无济于事.

这是我的wsdl:

<?xml version="1.0" enCoding="UTF-8"?>-<wsdl:deFinitions xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:wsam="http://www.w3.org/2007/05/addressing/Metadata" xmlns:soapenc="http://schemas.xmlsoap.org/soap/enCoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetnamespace="http://tempuri.org/" name="Service1">-<wsdl:types>-<xsd:schema targetnamespace="http://tempuri.org/imports"><xsd:import namespace="http://tempuri.org/" schemaLocation="http://localhost:51086/Service1.svc?xsd=xsd0"/><xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" schemaLocation="http://localhost:51086/Service1.svc?xsd=xsd1"/><xsd:import namespace="http://schemas.datacontract.org/2004/07/InsertIntoUserWCF" schemaLocation="http://localhost:51086/Service1.svc?xsd=xsd2"/></xsd:schema></wsdl:types>-<wsdl:message name="IService1_ksoapAdd_inputMessage"><wsdl:part name="parameters" element="tns:ksoapAdd"/></wsdl:message>-<wsdl:message name="IService1_ksoapAdd_OutputMessage"><wsdl:part name="parameters" element="tns:ksoapAddResponse"/></wsdl:message>-<wsdl:message name="IService1_addParam_inputMessage"><wsdl:part name="parameters" element="tns:addParam"/></wsdl:message>-<wsdl:message name="IService1_addParam_OutputMessage"><wsdl:part name="parameters" element="tns:addParamResponse"/></wsdl:message>-<wsdl:portType name="IService1">-<wsdl:operation name="ksoapAdd"><wsdl:input message="tns:IService1_ksoapAdd_inputMessage"    wsaw:Action="http://tempuri.org/IService1/ksoapAdd"/><wsdl:output message="tns:IService1_ksoapAdd_OutputMessage" wsaw:Action="http://tempuri.org/IService1/ksoapAddResponse"/></wsdl:operation>-<wsdl:operation name="addParam"><wsdl:input message="tns:IService1_addParam_inputMessage" wsaw:Action="http://tempuri.org/IService1/addParam"/><wsdl:output message="tns:IService1_addParam_OutputMessage" wsaw:Action="http://tempuri.org/IService1/addParamResponse"/></wsdl:operation></wsdl:portType>-<wsdl:binding name="BasichttpBinding_IService1" type="tns:IService1"><soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>-<wsdl:operation name="InsertUserDetails"><soap:operation  -<wsdl:operation name="ksoapAdd"><soap:operation  soapAction="http://tempuri.org/IService1/ksoapAdd"/>-<wsdl:input><soap:body use="literal"/></wsdl:input>-<wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation>-<wsdl:operation name="addParam"><soap:operation  soapAction="http://tempuri.org/IService1/addParam"/>-<wsdl:input><soap:body use="literal"/></wsdl:input>-<wsdl:output><soap:body use="literal"/></wsdl:output></wsdl:operation></wsdl:binding>-<wsdl:service name="Service1">-<wsdl:port name="BasichttpBinding_IService1" binding="tns:BasichttpBinding_IService1"><soap:address location="http://localhost:51086/Service1.svc"/></wsdl:port></wsdl:service></wsdl:deFinitions>

这是我的xsd2:

<?xml version="1.0" enCoding="UTF-8"?>-<xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/InsertIntoUserWCF"xmlns:xs="http://www.w3.org/2001/XMLSchema" targetnamespace="http://schemas.datacontract.org/2004/07/InsertIntoUserWCF" elementFormDefault="qualifIEd">-<xs:complexType name="testadd">-<xs:sequence><xs:element name="number_1" type="xs:int" minOccurs="0"/><xs:element name="number_2" type="xs:int" minOccurs="0"/></xs:sequence></xs:complexType><xs:element name="testadd" type="tns:testadd" nillable="true"/></xs:schema>

这是我的肥皂要求:

<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><ksoapAdd xmlns="http://tempuri.org/" ID="o0" c:root="1"><n0:num1 xmlns:n0="http://tempuri.org/"><number_1>25</number_1><number_2>25</number_2></n0:num1></ksoapAdd></v:Body></v:Envelope>

肥皂反应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ksoapAddResponse xmlns="http://tempuri.org/"><ksoapAddResult>0</ksoapAddResult></ksoapAddResponse></s:Body></s:Envelope>

这是我使用kso​​ap2的java代码:

private static final String nameSPACE = "http://tempuri.org/";private static final String URL = "http://10.0.2.2:51086/Service1.svc?wsdl";private static final String SOAP_ACTION4 = "http://tempuri.org/IService1/ksoapAdd";private static final String METHOD_name4 = "ksoapAdd";private static final String SOAP_ACTION5 = "http://tempuri.org/IService1/addParam";private static final String METHOD_name5 = "addParam";try {    testadd numX = new testadd(25,25);    SoapObject Request = new SoapObject(nameSPACE, METHOD_name4);    PropertyInfo prop = new PropertyInfo();    prop.setnamespace(nameSPACE);    prop.setname("num1");    prop.setType(numX.getClass());    prop.setValue(numX);    Request.addProperty(prop);    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);    envelope.dotNet = true;    envelope.setoutputSoapObject(Request);    envelope.implicitTypes = true;    httpTransportSE androIDhttpTransport = new httpTransportSE(URL);    androIDhttpTransport.deBUG=true;    androIDhttpTransport.call(SOAP_ACTION4, envelope);    Log.d(logtag + " request dump", androIDhttpTransport.requestDump);    Log.d(logtag + " response dump", androIDhttpTransport.responseDump);    final SoapPrimitive response = (SoapPrimitive)envelope.getResponse();    final int num = Integer.parseInt(response.toString());    } catch (Exception e)     {        e.printstacktrace();    }

这是我的java类testadd:

import java.util.Hashtable;import org.ksoap2.serialization.KvmSerializable;import org.ksoap2.serialization.PropertyInfo;public class testadd implements KvmSerializable{int number1;int number2;testadd(){}testadd(int a, int b){ number1 = a; number2 = b;}@OverrIDepublic Object getProperty(int arg0) {    switch(arg0)    {    case 0:        return number1;    case 1:        return number2;    }return null;}@OverrIDepublic int getPropertyCount() {    return 2;}@OverrIDepublic voID getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {     switch(arg0)        {        case 0:            arg2.type = PropertyInfo.INTEGER_CLASS;            arg2.name = "number_1";            break;        case 1:            arg2.type = PropertyInfo.INTEGER_CLASS;            arg2.name = "number_2";            break;        default:break;        }}@OverrIDepublic voID setProperty(int arg0, Object value) {    switch(arg0)    {    case 0:       number1 = Integer.parseInt(value.toString());        break;    case 1:        number2 = Integer.parseInt(value.toString());        break;    default:        break;    }}}

服务代码:

<ServiceContract()> _Public Interface IService1<OperationContract()> _Function ksoapAdd(ByVal num1 As testadd) As Integer<OperationContract()> _Function addParam(ByVal num1 As Integer, ByVal num2 As Integer) As IntegerEnd Interface<DataContract()> _Public Class testaddPrivate number1 As IntegerPrivate number2 As Integer<DataMember()> _Public Property number_1() As Integer    Get        Return number1    End Get    Set(ByVal value As Integer)        number1 = value    End SetEnd Property<DataMember()> _Public Property number_2() As Integer    Get        Return number2    End Get    Set(ByVal value As Integer)        number2 = value    End SetEnd PropertyEnd Class

服务方式:

Public Function ksoapAdd(ByVal num1 As testadd) As Integer Implements IService1.ksoapAdd    MsgBox(num1.number_1)    Return num1.number_1 + num1.number_2End FunctionPublic Function addParam(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Implements IService1.addParam    MsgBox(num1 + num2)    Return num1 + num2End Function

我知道这是很多代码,但它认为有必要了解整个图片.

谢谢!

解决方法:

我终于开始工作了!

原来我的androID ksoap2代码不喜欢WCF在SOAP服务中设置的默认命名空间(tempuri.org).

我的猜测是,使用默认命名空间,请求信封无法将传递的testadd对象映射到在服务中定义testadd类的datacontract.通过明确定义WCF服务中的servicecontract和datacontract的名称空间,这会更改/重新排列wsdl并使datacontract可访问.

我按照本教程摆脱了tempuri.org名称空间 –
http://blogs.msdn.com/b/endpoint/archive/2011/05/12/how-to-eliminate-tempuri-org-from-your-service-wsdl.aspx

我在服务代码(VB)的适当位置添加了以下内容

<ServiceContract(namespace:="http://wcfservicetest.org/Service1")> _<DataContract(namespace:="http://wcfservicetest.org/Service1")> _<ServiceBehavior(namespace:="http://wcfservicetest.org/Service1")> _

我将我的命名空间更改为“http://wcfservicetest.org/Service1”,并使用新的命名空间在androID java代码中编辑了nameSPACE和SOAP_ACTION.

这样做后,我得到了我想要的结果!

这是肥皂要求:

<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><ksoapAdd xmlns="http://wcfservicetest.org/Service1" ID="o0" c:root="1"><n0:num1 xmlns:n0="http://wcfservicetest.org/Service1"><number_1>25</number_1><number_2>25</number_2></n0:num1></ksoapAdd></v:Body></v:Envelope>

肥皂反应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><ksoapAddResponse xmlns="http://wcfservicetest.org/Service1"><ksoapAddResult>50</ksoapAddResult></ksoapAddResponse></s:Body></s:Envelope>

所以我建议其他人使用kso​​ap2学习SOAP服务,不要使用默认命名空间,设置自己的命名空间.我浪费了一周时间试图解决这个问题.

希望有一天能帮到某人!

总结

以上是内存溢出为你收集整理的使用kso​​ap2从android传递对象到wcf soap服务;它发送和接收0全部内容,希望文章能够帮你解决使用kso​​ap2从android传递对象到wcf soap服务;它发送和接收0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101934.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存