【发布时间】:2020-12-02 04:24:49
【问题描述】:
我有一个接收和返回复杂对象的 WCF 服务,我需要在 Android 中调用它。我已经阅读了很多如何使用 Ksoap 的内容,目前我的应用正在调用 WCF 服务,但 WCF 服务在请求对象的属性中接收到空值。
WCF 服务
[OperationContract]
WrAsignChkResponse CheckTrsRequest(WrAsignChkModel WrAsignData);
//Request and response objects in .Net
public class WrAsignChkModel
{
public string Arg1 { get; set; }
public string Arg2 { get; set; }
public string Arg3 { get; set; }
public string Arg4 { get; set; }
}
public class WrAsignChkResponse
{
public int ResponseCode { get; set; }
public string Message { get; set; }
public string RequestStatus { get; set; }
public string RequestTimeStamp { get; set; }
}
使用 KSoap2 的 Android 代码
private SoapSerializationEnvelope getWebServiceEnvelope(){
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = true;
return envelope;
}
private void callWebService(){
SoapObject body = new SoapObject(this.callHeader.getNamespace(), this.callHeader.getMethodName());
if (this.parameters != null && !this.parameters.isEmpty()) {
for (PropertyInfo param : this.parameters) {
body.addProperty(param);
}
}
SoapSerializationEnvelope envelope = this.getWebServiceEnvelope();
envelope.setOutputSoapObject(body);
envelope.addMapping(callHeader.getNamespace(), "WrAsignData", WrAsignChkModel.class);
HttpTransportSE transport = new HttpTransportSE(this.webServiceUrl, this.timeOut);
transport.debug = true;
transport.call(callHeader.getSoapAction(), envelope);
this.soapResponse = envelope.getResponse();
}
我在 Android 中也有实现 KVM Serializable 的请求对象。
public class WrAsignChkModel implements KvmSerializable {
private String Arg1;
private String Arg2;
private String Arg3;
private String Arg4;
//Getter and setters
@Override
public Object getProperty(int index) {
switch(index)
{
case 0:
return Arg1;
//...
}
return null;
}
@Override
public int getPropertyCount() {
return 4;
}
@Override
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
Arg1 = value.toString();
break;
//...
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Arg1";
break;
//...
default:
break;
}
}
}
HttpTransportSE 请求转储 通过此请求,WCF 服务将在 WrAsignData 对象中接收空值
<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>
<CheckTrsRequest xmlns="http://tempuri.org/">
<WrAsignData>
<Arg1>XXXX</Arg1>
<Arg2>XXXX</Arg2>
<Arg3>XXXX</Arg3>
<Arg4>XXXX</Arg4>
</WrAsignData>
</CheckTrsRequest>
</v:Body>
</v:Envelope>
我已经使用 Windows 窗体测试应用程序测试了 Web 服务,并且我已经捕获了该应用程序使用 wireshark 发送的 XML,我发现这是参数命名空间的差异。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<CheckTrsRequest xmlns="http://tempuri.org/">
<WrAsignData xmlns:a="http://schemas.datacontract.org/2004/07/Models.TrsApiModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Arg1>XXXX</a:Arg1>
<a:Arg2>XXXX</a:Arg2>
<a:Arg3>XXXX</a:Arg3>
<a:Arg4>XXXX</a:Arg4>
</WrAsignData>
</CheckTrsRequest>
</s:Body>
</s:Envelope>
互联网上没有很多关于如何正确使用 KSoap2 的信息,我也没有找到解决方案。有人可以帮我解决这个问题吗?
【问题讨论】:
标签: android wcf soap ksoap2 android-ksoap2