Liviu Sosu
Liviu Sosu

Reputation: 1579

Windows phone application does not work properly when I'm trying to call a web api

I am developing a simple windows phone 8 application.
I do not use MVVM, it is very simple.
I have a button, and when I press it I want to add some data into the database (using the post method).
The web API is made in asp.net and it works properly because I tested with fiddler, and also with a console application.
The code behind from the button is the following:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpClient client = new HttpClient();
        string baseUrl = "myWebApiLink";
        client.BaseAddress = new Uri(baseUrl);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        string serviceUrl;
        serviceUrl = "api/People";

        var anEmployee = new People()
         {
             FirstName = "Windows",
             LastName = "Phone",
             Age = 8
         };
         HttpResponseMessage response;
         response = client.PostAsJsonAsync(serviceUrl, anEmployee).Result; //the problem
         if (response.IsSuccessStatusCode)
         {
             //some success messages
         }
         else
         {
             //some fail messages
         }
UriKind.Relative));
    }

At the line

response = client.PostAsJsonAsync(serviceUrl, anEmployee).Result;  

the application just gets blocked.
It does not throw an exception, it just stays on that line.

Any ideas?

Thank you in advance!

Upvotes: 0

Views: 352

Answers (2)

Ajibola
Ajibola

Reputation: 1238

Actually i dont think it says on that line, you are calling an asynchronous method, which means your code continues executing and does not wait for the response. Changing the method signature by adding async, and then awaiting the respoonse from the call will work. Here is your code modified.

private async void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpClient client = new HttpClient();
        string baseUrl = "myWebApiLink/";
        client.BaseAddress = new Uri(baseUrl);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        string serviceUrl;
        serviceUrl = "api/People";

        var anEmployee = new People()
        {
            FirstName = "Windows",
            LastName = "Phone",
            Age = 8
        };

        var request = new HttpRequestMessage(HttpMethod.Post, serviceUrl);
        var requestContent = anEmployee; //this value is a string, check the format on your server

        request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");

        HttpResponseMessage response;
        response = await client.SendAsync(request); //no longer the problem

        if (response.IsSuccessStatusCode)
        {
            //some success messages
        }
        else
        {
            //some fail messages
        }
    }

Upvotes: 1

Muhammad Saifullah
Muhammad Saifullah

Reputation: 4292

use async button client and await on post async call... here is how.

private async void Button_Click(object sender, RoutedEventArgs e)
{
    HttpClient client = new HttpClient();
    string baseUrl = "myWebApiLink";
    client.BaseAddress = new Uri(baseUrl);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    string serviceUrl;
    serviceUrl = "api/People";
    var anEmployee = new People()
     {
         FirstName = "Windows",
         LastName = "Phone",
         Age = 8
     };
    HttpResponseMessage response;
    response = await client.PostAsJsonAsync(serviceUrl, anEmployee).Result;
    if (response.IsSuccessStatusCode)
    {
        //some success messages
    }
    else
    {
        //some fail messages
    }
}

Upvotes: 1

Related Questions