Wednesday, October 3, 2012

Android Code for calling .NET web service

You need to declare 4 variable to call web service method
  • OPERATION_NAME - name of method to call
  • NAMESPACE - namespace
  • SOAP_ACTION - particular methdo of specified namespace
  • URL - URL of your web serivce

Declare above variable as below :
  • String OPERATION_NAME="HelloWorld";
  • String NAMESPACE="www.tempuri.org/";
  • String SOAP_ACTION=NAMESPACE+OPERATION_NAME;
  • String URL="http://your-ip-address/your-application-nm(in .net)/Service.asmx"; ***

Note ***:  in above statement instead your-ip-address you have to write your own local pc ip address. you can find it from command prompt to invoke the ipconfig command. and specify your-application-nm which you specify at the time of making .NET web service.

Code to call HellowWorld method. (Default Method)

try{
   
      SoapObject request = new SoapObject(NAMESPACE,OPERATION_NAME);

      //below statement passes argument to the web method specify in web service.
      request.addProperty("name", "How r u?");   **

      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
      envelope.dotNet=true;
      envelope.setOutputSoapObject(request);
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
      androidHttpTransport.call(SOAP_ACTION, envelope);
      Object result = (Object)envelope.getResponse();
      status=result.toString();
      Toast.makeText(getBaseContext(),status,Toast.LENGTH-LONG).show();
}
catch(Exception e)
{
      Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}

NOTE ** : in this statement as first argument is as same as the name of the method argument you specified in .net and second argument the value of the argument.


Output :  HelloWorld How r u?
Explanation : HelloWorld from the method itself and second word which we pass from the ** statement.

0 comments:

Post a Comment