Páginas

Consuming OData Service with Xamarin Android

I'll show you a simple way of consuming OData Service with Xamarin Android

For those who don't know, OData is a standardized protocol for creating and consuming data APIs. OData builds on core protocols like HTTP and commonly accepted methodologies like REST. The result is a uniform way to expose full-featured data APIs.

We will use Simple.OData.Client. Simple.OData.Client is a multiplatform OData client library supporting .NET 4.x, Silverlight 5, Windows Phone 8, Android and iOS. The adapter provides a great alternative to WCF Data Services client. It does not require generation of context or entity classes and fits RESTful nature of OData services. The adapter has a NuGet package that can be installed from NuGet site.

Open Visual Studio and go to FILE --> NEW --> PROJECT and choose "Android Application"


This will open a new Android Application template.

As the "Getting started with Simple.OData.Client" article says, the easiest way to start using Simple.OData.Client is to install it’s Nuget package.

Now open the default activity file (Activity1.cs) and copy this code (NOTE that we have changed the inheritance activity from Activity to ListActivity).

What we are doing here is:
  1. On the OnCreate mehod, we just have initialized the ODataClient (client = new ODataClient("http://packages.nuget.org/v1/FeedService.svc/");)
  2. Then we have started an async task, so we dont block the UI, with method StartRestRequestAsync
  3. Last, we query the service client.FindEntries("Packages?$filter=Title eq 'Simple.OData.Client'"); iterate their result and add it to the ListAdapter
You can download a sample from https://bitbucket.org/hzaldivar/odataandroid