Xamarin Forms 101: How to consume REST API in Xamarin Forms

Are you looking for a quick or simple tutorial on how you can call a RESTful API service in Xamarin Forms?

Are you also searching for a way on how you can parse JSON data?

In this post, you will learn a simple way on how to consume and use REST API in Xamarin Forms.

 

Working on a REST API client is easy. Just follow this step-by-step process. Take note, the demo below is just a simple implementation of the tutorial. You can do a lot of things with Microsoft.Net.Http and Newtosoft.Json.

 

Step 1:

Search for Newtonsoft.Json and Microsoft.Net.Http NuGet packages.

Install Newtonsoft.Json NuGet package on each project in your solution

 

Install Microsoft.Net.Http ONLY in your PCL or .NET Standard library

 

Step 2:

Create the Model of your simple app.

Note: Read the comments and summaries that you can find in files below. They describe some methods and properties in this simple app.

 

 

Step 3:

Create the UI of this simple demo.

You need to create a ListView. ListView is a view for presenting lists of data, especially long lists that require scrolling.

 

 

Step 4:

Start creating your logic. In this case, we are doing a simple CRUD in the MainPage.xaml.cs (codebehind). This is just to make a quick a simple demo, you can do it in an MVVM pattern if you prefer it that way.

 

So what’s going on in this file? Let’s take a deeper look one by one.

This time I’ll put comments and summaries to explain what’s happening.

 

 

 

Here’s the online repository of this REST API with CRUD demo. GitHub URL: https://github.com/webmasterdevlin/Simple-Rest-Api-App-XF

 

Step 5:

Build and run the simple demo. Press the ADD, UPDATE, and DELETE buttons to see that they are working.

 

The best way to learn this calling and consuming a REST API service is to try it yourself. Happy coding!



One Comment

  • Tejashree

    finally I have got some working stuff for xamrin forms , awesome it working like a charm. Thanks man