I have an async method like so:
public async Task<LoginClass> LoginAsync(string email, string password)
{
var client = new HttpClient();
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("email", email),
new KeyValuePair<string, string>("password", password)
});
var response = await client.PostAsync(string.Format("/api/v1/login"), content);
var responseString = await response.Content.ReadAsStringAsync();
LoginClass items = JsonConvert.DeserializeObject<LoginClass>(responseString);
return items;
}
But when I call it in another method like so, its like my app freezes:
WebServiceClass webService = new WebServiceClass();
public LoginPage()
{
InitializeComponent();
}
protected async void OnLogin(System.Object sender, System.EventArgs e)
{
Task<LoginClass> response = webService.LoginAsync(Email.Text, Password.Text);
if(response.Result.error != null)
{
await DisplayAlert("Error", response.Result.error, "OK");
}
}
I put a break point on this line:
Task<LoginClass> response = webService.LoginAsync(Email.Text, Password.Text);
The break points, but after that nothing, it does not goto the next line. Its like its waiting for something.
What am I doing wrong and how do I properly call an async method?