Forum Xamarin.Forms

JSON Post request on button click

loginButton.Clicked += (object sender, EventArgs e) =>
Login(email.Text, password.Text);

On button Click this method is being called -

public void Login(string username, string password)
string url = "";

        string contentType = "application/json"; 
        JObject oJsonObject = new JObject();

        oJsonObject.Add("gender", "male");

        HttpClient oHttpClient = new HttpClient();
        var oTaskPostAsync = oHttpClient.PostAsync(url, new StringContent(oJsonObject.ToString(), Encoding.UTF8, contentType)).Result;

        if (oTaskPostAsync.IsSuccessStatusCode)
            DisplayAlert("Account Created", "Head back to the hompage and login with your new account", "Ok");

            DisplayAlert("Error Found", "", "Ok");

I am not able to return response I am getting in return. How can I print the response? Or How to create a async method for calling POST method on button click.

Best Answer


Sign In or Register to comment.