Xamarin – Rest Services from a PCL
I see this on a daily basis especially on the Xamarin forums.
How do I use this service from
- an iOS app?
- an Android app?
- a Windows app?
How do I write a PCL which I can then reuse on all platforms?
While I did present a solution to this issue in a previous article, that services part was only minor as the article was mainly about something else.
So here it is, an article dedicated to using Rest Services from a PCL.
Let’s start by creating the solution and the PCL project
next add a PCL which will target all the platforms you care about. I made mine cover them all, just in case.
Add the Microsoft HTTP Client Libraries Nuget package, this is the one which works from a PCL and for every platform we care about.
At this point we can start creating the service wrapper.
I am going to keep this very simple. We will demonstrate three calls :
a simple get with parameter, a json post with a model and a form post with the same model. This should cover the real life scenarios you need to implement a real app.
Finally, create an Android app and use the service wrapper to fire all the calls implemented.
Let’s have a look at the service wrapper interface:
As I said, it is very simple and only contains the three calls we want to show.
Now let’s have a look at its implementation:
While this implementation is very simple it does allow us to demonstrate the important scenarios we are likely to encounter in a real app. The get method is the simplest one, it passes some data in the Url and returns the response.
The next two calls build up their own content object. Both StringContent and FormUrlEncodedContent are built on top the abstract class HttpContent.
Please note how we’re telling the first call to set the Content-Type of the request to json.
The FormUrlEncodedContent class is a specialized implementation of HttpContent and sets its own Content-Type so you don’t have to worry about that one.
Now that we have the wrapper in place, it is time to use it from an app. So create a Android app, it will give you the default one where you click a button and it tells you how many times you clicked it. I simply modified this basic app a little bit and I am setting the button text to the response coming from the service.
Look inside the MainActivity.cs file:
As you can see I am calling each wrapper method in an async manner ( please note how I set the whole OnCreate method to be async so I can actually await every call. )
Every time I get a response I simply set the button text to that response. All you have to do is run the app and just watch the button text change. You don’t need to click at all.
This is the initial state of the app when no calls have run:
Wait a little bit and you should see the result of the first Get call:
Wait a bit more and here’s the json post call result:
Finally, after another very short delay we can see the result of the form post call:
These screen shots are from the android simulator. I happen to have an old Nexus 7 tablet, so I hooked it up to my computer and deployed the app to it, using Visual Studio. Here is the app running successfully on my Nexus 7 :
At this point I started to feel really bold, so I thought to myself …. hmm why not add a Windows phone app and run the same code there?
This would prove my point about the code truly being cross platform, plus there is one tiny little advantage in the form of me owning a Lumia 930 phone as well.
With this new purpose in mind, I added a Windows Phone 8.1 project, chucked a TextBlock on the first page ( MainPage.xaml ), copied over the code from the Android project, replaced the button text changes with the TextBlock changes, added in the references to the PCL libraries used by Android, ran the little beast and here’s the selfie to prove it :
Happy days, so now I have my beautiful PCL libraries running from both my Android Nexus 7 and Windows Lumia 930. I almost wish I had a real IPhone to add the iOS project as well, but unfortunately I don’t so I will leave this part up to someone who owns such a device.
Hopefully this will help you get over the initial hurdle of not knowing which client to use in order to work with a Rest api, or knowing which client to use but not how to actually use it.
Everything is built as a PCL, targeting Xamarin iOS, Android and Windows phone and it works the same way from every platform.
I have included the rest service as well. Currently it is deployed in my own Azure hosting environment, but this one it is not guaranteed to stay up forever. You have the service code so if mine happens to be down simply deploy it somewhere of your own choosing and update the ServiceWrapper class with the new service URL.
The full source code is available on GitHub
As usual, if you have any questions / comments then please let me know.
This article has been posted on LinkedIn as well