Pages

Subscribe:

Monday, March 14, 2011

Android - Consuming Dot Net Web Services in Android

How to call dot net we services in Android?


Using dot net web services in Android is simple and straight forward.  Following example demonstrates accessing dot net web services in Android.

1. Create a simple HelloWorld Web Service in Dot Net. Specify valid namespace in the web service. 

namespace SampleWebService
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://<YourWebServiceURL/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
        
    public class WebService : System.Web.Services.WebService
    {
        Class1 _Class1Obj = new Class1();
        [WebMethod]

        public string HelloWorld()
        {
            return "Hello World";
        }
              
    }
}


The above web service returns a string to the calling service.

Now create the Android Client.

Declare the Namespace and URL for the web services globally. Refer the server and your asmx file here.

private static final String NAMESPACE = "http://<server.com>/";
private static final String URL = "http://<server.com>/yourfile.asmx";


Assuming one button and text box in the screen. Create the action for the button.


btnHelloWorld = (Button) findViewById(R.id.btnHelloWorld);
btnHelloWorld.setOnClickListener((OnClickListener) new HelloWorld());

Now, create the following two methods to create and call the web services.

public SoapObject getSoapRequest(String methodname) {
return new SoapObject(NAMESPACE, methodname);
}

public SoapSerializationEnvelope getEnvelope(SoapObject Request) {
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
return envelope;
}

public String getResponse(String SOAP_ACTION,
SoapSerializationEnvelope envelope) {
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
URL);
Log.d("WEB_SERVICE", "transport object created");
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
Log.d("WEB_SERVICE", "Action called");
Log.d("WEB_SERVICE", "got response");
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String result = response.toString();
return result;
} catch (XmlPullParserException e) {
Log.d("WEB_SERVICE", e.toString());
} catch (Exception e) {
Log.d("WEB_SERVICE", e.toString());
}
return null;
}


Now, create the following method for the button onclick. You need to specify the soap action in the method.

class HelloWorld implements OnClickListener {

@Override
public void onClick(View v) {
Log.d("WEB_SERVICE", "Started web call method");
String SOAP_ACTION = "http://<your server>/<your-soap-action>";
String METHOD_NAME = "HelloWorld";
SoapObject Request = getSoapRequest(METHOD_NAME);
SoapSerializationEnvelope envelope = getEnvelope(Request);

Log.d("WEB_SERVICE", "enveloper created");

String result = getResponse(SOAP_ACTION, envelope);

Log.d("WEB_SERVICE", "printing the response");
Log.d("WEB_SERVICE", result);
lblResponse.setText(result);

}
}


To work with web services please download the ksoap library from the below url.


You can also use php web services in Android with same logic. Wish you happy web services with Android and ksoap.

1 comments:

Victor said...

By Combining the Domain expertise with Low Cost Offshore .NET Development process, CATT Ltd’s Offshore development Model delivers Enhanced Productivity with Quality and Cost Savings. CATT Ltd offers its customers an option of having a Dedicated offshore .NET Developers to augment their IT capabilities by proving access to their Talented professionals and the state-of-the-art infrastructure.

Post a Comment