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 »

  1. What you have before was easy (i work on c#), but for the code below i really don’t know how to finish it. I learn Android and i try to find what to put in build request. But i failed . I just need an example. All the time it is the same problem, people only explain half. And i never two good half (from c# and from android)…

    Thank you for your help

    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, (too simple for me, unusuable)

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

    try {

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

    … ???

    Comment by qq | December 17, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: