使用ksoap2从android向WCFSOAP服务传递对象;它发送和接收0
我试图使用ksoap2从android访问WCFSOAP服务中的方法。 我能够成功地将简单的类型参数传递给该方法,它工作得很好。 但是当我尝试传递对象时,wcf方法接收到所有对象值的0 我在WP7应用程序中使用了相同的SOAP服务,并且运行良好 我的服务中有两个方法,ksoapadd和addParam。addParam接受两个整数并返回它们的和(这是有效的)。ksoapadd接受一个类testadd的对象,该对象有两个整数元素,ksoapadd返回它们的和(这获取并发送0) 我认为这与名称空间有关,但我不知道如何解决它。我一直在做反复试验以使soap请求正确,但还没有成功 谁能帮我想一想怎么办?我尝试了许多方法,不知何故帮助了其他有类似问题的人,但都没有用 以下是我的wsdl:使用ksoap2从android向WCFSOAP服务传递对象;它发送和接收0,android,vb.net,wcf,soap,ksoap2,Android,Vb.net,Wcf,Soap,Ksoap2,我试图使用ksoap2从android访问WCFSOAP服务中的方法。 我能够成功地将简单的类型参数传递给该方法,它工作得很好。 但是当我尝试传递对象时,wcf方法接收到所有对象值的0 我在WP7应用程序中使用了相同的SOAP服务,并且运行良好 我的服务中有两个方法,ksoapadd和addParam。addParam接受两个整数并返回它们的和(这是有效的)。ksoapadd接受一个类testadd的对象,该对象有两个整数元素,ksoapadd返回它们的和(这获取并发送0) 我认为这与名称空间有
<?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 style="document"
-<wsdl:operation name="ksoapAdd">
<soap:operation style="document" 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 style="document" 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>
这是我的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;}
@Override
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return number1;
case 1:
return number2;
}
return null;
}
@Override
public int getPropertyCount() {
return 2;
}
@Override
public 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;
}
}
@Override
public 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 Integer
End Interface
<DataContract()> _
Public Class testadd
Private number1 As Integer
Private number2 As Integer
<DataMember()> _
Public Property number_1() As Integer
Get
Return number1
End Get
Set(ByVal value As Integer)
number1 = value
End Set
End Property
<DataMember()> _
Public Property number_2() As Integer
Get
Return number2
End Get
Set(ByVal value As Integer)
number2 = value
End Set
End Property
End Class
我知道这是很多代码,但它认为有必要了解整个情况
谢谢大家! 我终于成功了 我的android ksoap2代码不喜欢WCF在SOAP服务中设置的默认名称空间(tempuri.org) 我的猜测是,使用默认名称空间,请求信封无法将传递的TestAddd对象映射到服务中定义TestAddd类的datacontract。通过在WCF服务中显式地为servicecontract和datacontract定义名称空间,这将更改/重新排列wsdl并使datacontract可访问 我遵循本教程摆脱了tempuri.org名称空间- 我在服务代码(VB)的适当位置添加了以下内容
_
_
_
我将名称空间更改为“http://wcfservicetest.org/Service1并使用新名称空间编辑了android java代码中的名称空间和SOAP_操作
这样做之后,我得到了我想要的结果
以下是soap请求:
<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>
25
25
soap响应:
<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>
<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>
50
因此,我建议其他使用ksoap2学习SOAP服务的人,不要使用默认名称空间,设置自己的名称空间。我浪费了一个星期的时间想弄明白这一点
希望有一天这能帮助别人 仅仅是因为代码的数量和良好的格式,你值得一次投票…但是今天我的代码已经用完了…这是我的最后一个…谢谢你,我希望有人能尽快给我一些意见。天哪,你完全救了我,我被困了一个多星期!谢谢:)
<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 Integer
End Interface
<DataContract()> _
Public Class testadd
Private number1 As Integer
Private number2 As Integer
<DataMember()> _
Public Property number_1() As Integer
Get
Return number1
End Get
Set(ByVal value As Integer)
number1 = value
End Set
End Property
<DataMember()> _
Public Property number_2() As Integer
Get
Return number2
End Get
Set(ByVal value As Integer)
number2 = value
End Set
End Property
End Class
Public Function ksoapAdd(ByVal num1 As testadd) As Integer Implements IService1.ksoapAdd
MsgBox(num1.number_1)
Return num1.number_1 + num1.number_2
End Function
Public Function addParam(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Implements IService1.addParam
MsgBox(num1 + num2)
Return num1 + num2
End Function
<ServiceContract(Namespace:="http://wcfservicetest.org/Service1")> _
<DataContract(Namespace:="http://wcfservicetest.org/Service1")> _
<ServiceBehavior(Namespace:="http://wcfservicetest.org/Service1")> _
<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>