JG Vimalan's Blog

It is all about coding!

Consuming WCF service in Android application

In order to consume WCF service in an Android application, the WCF Service must send and receive data in JSON format. So, you need to set the RequestFormat and ResponseFormat to JSON, in the WebInvoke attribute as shown below,

using System.ServiceModel;
using System.ServiceModel.Web;

    [ServiceContract]
    public interface IService
    {      
        [OperationContract]
        [WebInvoke(Method = “POST”,
        UriTemplate = “/GetEmployee”,
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json)]
        Employee GetEmployee(EmployeeIdentity request);

I used wsHttpBinding in my WCF service.  

Now, to consume the WCF Service in your Android application, use httpClient to process the request and get the response. A sample code is given below,

HttpClient httpClient = null;
HttpUriRequest httpUriRequest = null
HttpResponse httpResponse = null;

try {
      
httpUriRequest = //build request
httpClient = new DefaultHttpClient();
httpResponse = httpClient.execute(httpUriRequest);

Advertisements

October 29, 2010 Posted by | Android, JSON, WCF | 1 Comment