Skip to content Skip to sidebar Skip to footer

How To Pass Parameter To A Webservice Using Ksoap2?

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 work

Solution 1:

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.

Solution 2:

Calling webservice by passing parameters from j2me

SoapObjectrequest=newSoapObject("http://www.webserviceX.NET", "GetCitiesByCountry");
StringsoapAction="http://www.webserviceX.NET/GetCitiesByCountry";

request.addProperty("CountryName", "india");
SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;

HttpTransportht=newHttpTransport("http://www.webservicex.net/globalweather.asmx");
ht.debug = true;
//System.err.println( ht.requestDump );

ht.call(soapAction,envelope);
System.out.println("####################: " +envelope.getResponse());
//SoapObject result = (SoapObject)envelope.getResponse();

Solution 3:

I have been working with this for 2 days now and i finally got the solution. I submit my complete code and hope this will help. It Can pass Parameters and get response.

Inside the WebService file in .net C#:

[WebService(Namespace = "http://something/webservice/v1")]

[WebMethod]
public DateTime[] Function(Guid organizationId, Guid categoryId)
    {
        return ...;
    }

Inside the Android code:

private final staticStringURL = "http://something/WebServices/WebService.asmx";
private final staticStringNAMESPACE = "http://something/webservice/v1";


publicArrayList<Object> getSoapObject(String METHOD_NAME, String SOAP_ACTION, Map<String, String> parameters){

try {

        ArrayList<Object> sol = newArrayList<Object>();
        SoapObject request = newSoapObject(NAMESPACE, METHOD_NAME);

        if(parameters != null){
            for (Entry<String, String> para : parameters.entrySet()) {
                request.addProperty(para.getKey(), para.getValue());
            }
        }

        SoapSerializationEnvelope envelope = newSoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet=true;
        envelope.setOutputSoapObject(request);

        Log.d("Body", envelope.bodyOut.toString());

        HttpTransportSE androidHttpTransport = newHttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result=(SoapObject)envelope.getResponse();

            for(int i = 0; i < result.getPropertyCount(); i++){
                sol.add(result.getProperty(i));
            }

            return sol;
        } catch (Exception e) {
            e.printStackTrace();
            returnnull;
        }       
    }


    publicvoidgetMenuEndDate(String orgId, String categoryId){

        Date startDate = null;
        Date endDate = null;

        HashMap<String, String> parameters = newHashMap<String, String>();
        parameters.put("organizationId", orgId);
        parameters.put("categoryId", categoryId);


        ArrayList<Object> sol = getSoapObject("Function", "http://something/webservice/v1/Function", parameters);

        SimpleDateFormat dateFormatter = newSimpleDateFormat("yyyy-MM-dd");

        try {
            startDate = (Date)dateFormatter.parse(sol.get(0).toString());
            endDate = (Date)dateFormatter.parse(sol.get(1).toString());
        } catch (ParseException e) {
            Log.d(TAG, "Exception i Date-Formatering");
            e.printStackTrace();
        }

    }

Things to check:

  • Are the parameters named exactly the same as what is expected in the Web Service?
  • Check if you use Trailing "/" for the Namespace. Have the same in you application.

Solution 4:

Try commenting out the line:

envelope.dotNet=true;

I did the same thing you did and when I read about this property being a really ugly hack, I commented it out for testing purposes and my parameter got passed correctly.

Solution 5:

You will have to declare parameter type in client code:

SoapObjectrequest=newSoapObject("http://tempuri.org/", "mymethod"); 
PropertyInfop=newPropertyInfo();
p.setName("param_name_from_webservice");
p.setValue(true);
p.setType(Boolean.class);
request.addProperty(p);

Post a Comment for "How To Pass Parameter To A Webservice Using Ksoap2?"