← Back
archive

Xamarin.Native Tutorial - Consuming WebService - Setup

Xamarin.Native Tutorial - Consuming WebService - Setup

Xamarin.Native Tutorial - Consuming WebService - Setup

Alright, let’s consume a Webservice Next.

We will consume a sample RESTful webservice (made using ASP.NET WebAPI) from rabbit.miaw.xyz/api.

Setup Install Newtonsoft.Json First, open NuGet package manager. Install the package Newtonsoft.Json.

Create a Method to Get Data

Open Repository.cs.

Add this method:

 public async System.Threading.Tasks.Task<IEnumerable<Invoice>> GetInvoicesFromWebserviceAsync()
        {
            IEnumerable<Invoice> invoices = new List<Invoice>();

            using (HttpClient client = new HttpClient())
            {
                //client.BaseAddress = new Uri("http://rabbit.miaw.xyz/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync("http://rabbit.miaw.xyz/api/invoices");
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    invoices = JsonConvert.DeserializeObject<IEnumerable<Invoice>>(result);
                }
            }

            return invoices;
        }

…And reference the namespaces as necessary.

Alright, now we have setup our code, let’s actually try actually consuming the webservice and show it the data to our users next.

UWP: In progress

Android: http://miaw.xyz/b/post/2017/09/20/xamarin-native-tutorial-consuming-webservice-android

iOS: In progress

Privacy notice

We value your privacy

We use cookies and similar technologies to support site functionality and, where applicable, advertising. You can accept or decline optional consent and update your choice later from the privacy page.