Xamarin Forms Consuming Rest Webservice JSON Parsing C Xaml
Introduction:
Previously we learned XML parsing in Xamarin.Forms. And this article demonstrates how to consume a RESTful web service and how to parse the JSON response from a Xamarin.Forms application.


Previously we learned XML parsing in Xamarin.Forms. And this article demonstrates how to consume a RESTful web service and how to parse the JSON response from a Xamarin.Forms application.

Requirements:
- This article source code is prepared by using Visual Studio 2017 Enterprise. And it is better to install latest visual studio updates from here.
- This article is prepared on a Windows 10 machine.
- This sample project is Xamarin.Forms PCL project.
- This sample app is targeted for Android, iOS & Windows 10 UWP. And tested for Android & UWP only. Hope this sample source code would work for iOS as well.
Description:
This article can explain you below topics:
1. How to create Xamarin.Forms PCL project with Visual studio 2017?
2. How to check network status from Xamarin.Forms app?
3. How to consuming webservice from Xamarin.Forms?
4. How to parse JSON string?
5. How to bind JSON response to ListView?
Lets learn how to use Visual Studio 2017 to create Xamarin.Forms project.
1. How to create Xamarin.Forms PCL project with Visual studio 2017?
Before to consume webservice, first we need to create the new project.
- Launch Visual Studio 2017/2015.
- On the File menu, select New > Project.
- The New Project dialog appears. The left pane of the dialog lets you select the type of templates to display. In the left pane, expand Installed > Templates > Visual C# > Cross-Platform. The dialogs center pane displays a list of project templates for Xamarin cross platform apps.
- In the center pane, select the Cross Platform App (Xamarin.Forms or Native) template. In the Name text box, type "RestDemo". Click OK to create the project.
- And in next dialog, select Blank App=>Xamarin.Forms=>PCL.The selected App template creates a minimal mobile app that compiles and runs but contains no user interface controls or data. You add controls to the app over the course of this tutorial.
- Next dialog will ask for you to confirm that your UWP app support min & target versions. For this sample, I target the app with minimum version 10.0.10240 like below:
2. How to check network status from Xamarin.Forms app?
Before call webservice, first we need to check internet connectivity of a device, which can be either mobile data or Wi-Fi. In Xamarin.Forms, we are creating cross platform apps, so the different platforms have different implementations.
So to check the internet connection in Xamarin.Forms app, we need to follow the steps given below.
Step 1:
Go to solution explorer and right click on your solution=>Manage NuGet Packages for solution.


Now search for Xam.Plugin.Connectivity NuGet package. On the right side, make sure select all platform projects and install it.
Step 2:
In Android platform, you have to allow the user permission to check internet connectivity. For this, use the steps given below.
Right click on RestDemo.Android Project and select Properties => Android Manifest option. Select ACCESS_NETWORK_STATE and INTERNET permission under Required permissions.
Step 3:
Create a class name "NetworkCheck.cs", and here I placed it in the Model folder. After creating a class, add below method to find network status.
- namespace RestDemo.Model
- {
- public class NetworkCheck
- {
- public static bool IsInternet()
- {
- if (CrossConnectivity.Current.IsConnected)
- {
- return true;
- }
- else
- {
- // write your code if there is no Internet available
- return false;
- }
- }
- }
- }
3. How to consuming webservice from Xamarin.Forms?
We can consume webservice in Xamarin using HttpClient. But it is not directly available, and so we need to add "Microsoft.Net.Http" from Nuget.
Step 1: Go to solution explorer and right click on your solution=>Manage NuGet Packages for a solution => search for Microsoft.Net.Http NuGet Package=>on the right side, make sure select all platform projects and install it.
Note: To add "Microsoft.Net.Http", you must install "Microsoft.Bcl.Build" from Nuget. Otherwise, you will get an error like "Could not install package Microsoft.Bcl.Build 1.0.14. You are trying to install this package into a project that targets Xamarin.iOS,Version=v1.0, but the package does not contain any assembly references or content files that are compatible with that framework."
Step 2:
Now it is time to use HttpClient for consuming webservice and before that we need to check the network connection. Please note that In below code you need to replace your URL, or you can also find the demo webservice url from the source code given below about this article.
- public async void GetJSON()
- {
- // Check network status
- if (NetworkCheck.IsInternet())
- {
- var client = newlink download