Veeresh Kumar

Veeresh Kumar

  • NA
  • 51
  • 14k

Fatch Data from local Host Api in Xamarin Forms

Nov 29 2018 7:30 AM
This is my Code and i Can't fatch data from api in xamarin.  
 
 
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
public static List<data> getPersons()
{
List<data> lp = new List<data>();
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = client.GetAsync("http://localhost:51949/api/employee").Result;
if (response.IsSuccessStatusCode)
{
lp = JsonConvert.DeserializeObject<data[]>(response.Content.ReadAsStringAsync().Result).ToList();
}
}
catch
{
}
return lp;
}
private async void Button_Clicked(object sender, EventArgs e)
{
var list = getPersons();
await Navigation.PushAsync(new ListOfEmployee());
}
}
 

Answers (2)