如何使用 ksoap2 将参数传递给网络服务? [英] How to pass parameter to a webservice using ksoap2?

查看:21
本文介绍了如何使用 ksoap2 将参数传递给网络服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Eclipse IDE 开发一个 android 应用程序.我正在尝试连接到 .net 网络服务.我正在使用 ksoap2 2.3 版

I'm using Eclipse IDE to develop an android app. I'm trying to connect to a .net webservice. I'm using ksoap2 version 2.3

当我调用一个没有参数的 webmethod 时,它工作正常.当我将参数传递给 webmethod 时,我得到 null(在调试 webservice 时我发现了这一点)并且我从客户端代码中的 webmethod 得到了 null.

When I'm calling a webmethod with no parameters, it works fine. When I come to pass a parameter to the webmethod, I get null (while debugging the webservice I discovered that) and I get a null from the webmethod in the client side code.

代码:

package com.examples.hello;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
 private static final String SOAP_ACTION = "http://Innovation/HRService/stringBs";

 private static final String METHOD_NAME = "stringBs";

 private static final String NAMESPACE = "http://Innovation/HRService/";
 private static final String URL = "http://196.205.5.170/mdl/hrservice.asmx";
 TextView tv;

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     tv=(TextView)findViewById(R.id.text1);
     call();

 }

 public void call()
 {
         try {

          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
          //PropertyInfo PI = new PropertyInfo();

             //request.addProperty("a", "myprop");

             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
             envelope.setOutputSoapObject(request);

             envelope.dotNet=true;
             envelope.encodingStyle = SoapSerializationEnvelope.XSD;


             HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

             androidHttpTransport.call(SOAP_ACTION, envelope);

             Object result = (Object)envelope.getResponse();


             String results = result.toString();
             tv.setText( ""+results); 
         } catch (Exception e) {
             tv.setText(e.getMessage());
             }
     }


}

为什么我得到空响应,如何使用 ksoap2 将参数传递给网络服务?

Why do I get the null response, how do I pass a parameter to a webservice using ksoap2?

推荐答案

代替

    request.addProperty("a", "myprop"); 

尝试使用

    request.addProperty("arg0", "myprop");         

我不是对 ksoap2 的期望,但我很确定这将第一个参数的值设置为您的 Web 服务功能.非常适合我.

I'm not an expect on ksoap2 but i'm pretty sure this sets the value of the first parameter to your web service function. Has worked perfectly for me.

这篇关于如何使用 ksoap2 将参数传递给网络服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆