简体   繁体   中英

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!

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
        }
    }

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM