I'am trying to get data from my simple WebApi web service. In Android Xamarin application i've got very simple code:
public class Persons
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
class WebRequests
{
public static List<Persons> getPersons()
{
List<Persons> lp = new List<Persons>();
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
HttpResponseMessage response = client.GetAsync("http://localhost:49814/api/Data/Persons").Result;
if (response.IsSuccessStatusCode)
{
lp = JsonConvert.DeserializeObject<Persons[]>(response.Content.ReadAsStringAsync().Result).ToList();
}
}
catch
{
}
return lp;
}
}
Problem is with line
HttpResponseMessage response = client.GetAsync("http://localhost:49814/api/Data/Persons").Result;
Because i'am receiving unhandler error. I don't know whats is wrong, because there is no any error message i catch statement. I was testing this code in a simple Win Forms application and everything was fine. For testing Xamarin app I'am using build-in emulator. Maybe should I use a real device for testing?