WebServices, Xamarin

Getting started with Web Service consuming using Xamarin Forms

For months, I have seen a lot of people asking about how to integrate a database with Xamarin Forms or what is the most recommend database to use, among other questions.  Now, I will show you that it’s possible to consume any data you want, without the need for a database connection. How? You just have to consume a Web Service that gives you the data you need. You can know more information about Web Service here.

First of all… What do I need?

  • Add from NuGet Package the plugin: Microsft.Net.Http

  • Add from NuGet Package the plugin: Newtonsoft.Json

Let’s Start!

The first that you have to get is the URL from your Web Service. In this case, I will use the following example: https://jsonplaceholder.typicode.com/posts/1

Creating the Entity class

After that, let’s create the entity class of our Web Service. We can create it by ourselves or in an automatical way like the following:

Enter to: http://json2csharp.com/

This site will generate the following entity class:

In your project, you must create a .cs file, in this example my class is named WSResult.cs.Inside this class, you will paste the code generate bellow and you will replace the name of the generated class (RootObject) with your class name.

Resuming this class, you must have the following source:

Creating the class to read your Web Service data

Here you have to use HttpClient Class, with this class, you will be able to access to your Web Service information. Finally,  you have to deserialize your object.

Getting the data

Here you just have to invoke the method created below with the required parameters and start to read your values.

Displaying your XAML

Good luck with your implementation! ?

Click to see an example in my GitHub.

Spanish post: https://medium.com/@reyes.leomaris/consumiento-web-service-en-xamarin-forms-1172f360a7c8

References: https://www.youtube.com/watch?v=xNP-K37mssA&app=desktop

Tagged , ,

2 thoughts on “Getting started with Web Service consuming using Xamarin Forms

Leave a Reply

Your email address will not be published. Required fields are marked *