How to call Synchronous API?

Hi,
My Xamarin.Android Login page needs to login from API. Now as I am calling the API Asynchronously, I am getting null value exception erorr . But i test my code in Asp.net mvc and Worked it but it don't work in xamarin.android .
My code in asp.net mvc
var client = new HttpClient { BaseAddress = new Uri("url") };

        var jsonStrResult = client.PostAsync("/login", new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("username", "c1"),
                new KeyValuePair<string, string>("password", "1234"),
                new KeyValuePair<string, string>("grant_type", "password")
            }))
            .Result.Content.ReadAsStringAsync().Result;

        var objDic = Newtonsoft.Json.JsonConvert.DeserializeObject<IDictionary<string, string>>(jsonStrResult);

My code in xamarin.android

var httpClient = new HttpClient { BaseAddress = new Uri("url") };
        var response =await httpClient.PostAsync("/login", new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("username", "c1"),
            new KeyValuePair<string, string>("password", "1234"),
            new KeyValuePair<string, string>("grant_type", "password")
        }));
        if (response.IsSuccessStatusCode)
        {

            string responseData = await response.Content.ReadAsStringAsync();
        }
Tagged:

Answers

  • MarlonRibeiroMarlonRibeiro USMember ✭✭✭
             var jsonStrResult = await client.PostAsync("/login", new FormUrlEncodedContent(new[]
                        {
                            new KeyValuePair<string, string>("username", "c1"),
                            new KeyValuePair<string, string>("password", "1234"),
                            new KeyValuePair<string, string>("grant_type", "password")
                        }));
            var response = await jsonStrResult .Content.ReadAsStringAsync();
    
Sign In or Register to comment.