Monday, February 25, 2013

Introduction

This post will briefly describe how to implement WCF web service client side in an Android application. For this purpose, simple “ToDo” Android application will be used for demonstration. The complete source code can be downloaded from here.
The easiest way to do this is by using WCF Rest services that will send information in JSON or XML format. If you would like to use SOAP base services, then you would need to use some 3rd party libraries for Android like kSOAP.

Implementing WCF web service

In this post, we’ll use Azure project approach to implement web service, but any other approach will serve the purpose also.
The first step is to create new Azure Cloud Service project. If you don’t have Azure SDK installed, you can download it from Azure web site. In .NET section you’ll find installs both for VS2012 and VS2010.
In Visual Studio, go File > New Project and under the Cloud category choose Windows Azure Cloud Service project. Fill in necessary data, hit Ok and afterwards select WCF Service Web Role.

Step 01: Select Azure project

Step 02: Select WCF Service Role

After you created your project, add a new WCF service or refactor default WCF service that has been generated within the project. Replace service interface with the following code:

AddToDoItem – will be used for adding a new ToDo item. It will receive a ToDo item in JSON format (RequestFormat), through POST request.

GetToDoItemsByDate – will be used to retrieve all ToDo items for specific date. Method receives date as an argument in JSON format, through GET request and retrieves list of ToDo items also in JSON format (ResponseFormat).

GetAllToDoItems – will be used to retrieve all ToDo items. The list of items in JSON format will be in response body for GET request.

The concrete implementation contains only SQL stored procedure calls. With these stored procedures, data are stored in and retrieved from MSSQL database. Also, DBConnection class defines DB connection and connection string and it looks like this:

The last thing concerning WCF service implementation is configuration within WebConfig file in your project. Under <system.serviceModel>, in your config file, you’ll need to make the following changes:

<system.serviceModel><behaviors><endpointBehaviors><behaviorname="httpBehavior"><dataContractSerializermaxItemsInObjectGraph="10000000"/><webHttp/></behavior></endpointBehaviors><serviceBehaviors><behavior><!-- To avoid disclosing metadata information, set the value below to false before deployment --><serviceMetadatahttpGetEnabled="true"/><!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --><serviceDebugincludeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors><serviceHostingEnvironmentmultipleSiteBindingsEnabled="true"aspNetCompatibilityEnabled="true"/><services><servicename="WCFServiceWebRole1.ToDoService"><endpointaddress="web"behaviorConfiguration="httpBehavior"binding="webHttpBinding"contract="WCFServiceWebRole1.IToDoService"/>
<endpoint address=""
binding="basicHttpBinding"
contract="WCFServiceWebRole1.IToDoService" />
</service></services></system.serviceModel>

Notice, that you’ll need to add <behaviorname="httpBehavior"> and service endpoints under <services>. For purpose of demonstration, you can notice two endpoints with different bindings: basic and web. For current implementation, you can only use webHttpBinding, but in the case you have a legacy WCF service with different binding, you can simply add another endpoint for your new requirement and WCF service will continue to work with the previous and the new binding as well.

Implementing Android WCF client

In this section, it will be only presented the code for communication with previously implemented web service. For complete Android application you can refer to complete project from this link.

Since we are using Rest services the client can be implemented by using HttpRequests. The following code represents the implementation of client side in Android application for each web service’s method call:

NOTICE: You can see the service URL is directed to 10.0.2.2, and use this IP address since it is the address of the host that runs the WCF service. Do not use 127.0.0.1 since that would be the address of your Android phone or emulator that runs the application. Also, after you run your Azure project on the emulator, check the port number in your URL. Usually, it is 81, but if it is different you’ll need to change it to the right one.

For the newest versions of Android SDK you’ll need to use the AsyncTask class for any kind of connection to remote resource. After you extend this class you’ll override the doInBackground method that receives the array of arguments. This method will proxy your request to specific web service call.