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
