How to pass parameter to a webservice using ksoap2?

user255681 picture user255681 · Jan 21, 2010 · Viewed 44.1k times · Source

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

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.

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());
             }
     }


}

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

Answer

Chris picture Chris · Jun 1, 2010

Instead of

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

try using

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

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.