使用带有复杂对象的KSoap2调用WCF服务 WCF收到空值

如何解决使用带有复杂对象的KSoap2调用WCF服务 WCF收到空值

我有一个WCF服务,该服务接收并返回comples对象,我需要在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可序列化的请求对象。

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 Forms测试应用程序测试了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>

Internet上没有很多有关如何与KSoap2一起正常工作的信息,我也找不到解决方案。有人可以帮我吗?

解决方法

尝试使用Datacontract和DataMember:

Concatenate

如果问题仍然存在,请随时告诉我。

更新

我也认为这是一个序列化问题。我不知道您怎么称呼该服务。如果我们通过添加服务引用来调用它,则客户端类应如下所示:

enter image description here

,

经过数小时的调查,我已经解决了。我将在相同情况下发布答案以帮助他人。

使用SoapUI,我已经看到要发送的名称空间和XML,然后尝试使用KSoap2生成类​​似的内容。

在我的课堂上,我在getPropertyInfo方法中添加了名称空间。所有属性都使用相同的名称空间,这将生成一个XML,其中所有标签中都包含该名称空间。与我在问题中提到的XML不同。没关系,而且可以兼容。

@Override
public void getPropertyInfo(int index,Hashtable properties,PropertyInfo info) {
    switch(index)
    {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "Arg1";
            info.namespace = "http://schemas.datacontract.org/2004/07/Models.TrsApiModel"
            break;
        //...
        default:
            break;
    }
}

然后在我的课堂上,我添加了一种生成SoapObject的方法。名称空间与对象内部的属性不同。

public SoapObject getSoapRequest(String nameSpace,String name){
    SoapObject soapRequest = new SoapObject(nameSpace,name);
    for(int i = 0; i < this.getPropertyCount(); i++){
        PropertyInfo prp = new PropertyInfo();
        this.getPropertyInfo(i,null,prp);
        prp.setValue(this.getProperty(i));
        soapRequest.addProperty(prp);
    }
    return soapRequest;
}

否则,您有要发送的SoapObject,您必须使用addSoapObject方法将int添加到Body SoapObject。

SoapObject body = new SoapObject(this.callHeader.getNamespace(),this.callHeader.getMethodName());
//soapObj is an instance using the getSoapRequest() method mentioned in my piece of code
body.addSoapObject(soapObj);
SoapSerializationEnvelope envelope = this.getWebServiceEnvelope();
envelope.setOutputSoapObject(body);

使用该代码,所有内容似乎都可以正常运行,但并不完全。我已经体验到服务器可以很好地接收某些参数,但并非所有参数都可以。还有一点棘手的事情是,对象属性必须与SoapUi请求中的参数具有相同的顺序。因此,您必须修改对象方法中的开关,以使索引具有与SoapUI XML请求中看到的顺序相同的顺序。

与索引注意顺序相同的方法

public Object getProperty(int index)
public void setProperty(int index,Object value)
public void getPropertyInfo(int index,PropertyInfo info)

我花了很多时间才能发现WCF如何如此详细地接收正确的参数。因此,希望我的回答对遇到同样问题的人有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-