0

I need to get list of objects deserialized from Json.

My problem is that this code hangs in line responseMessage = await httpClient.GetAsync(uri); I have checked get and response in Fiddler, I am getting Json in Fiddler, everything has code 200 OK, but for some reason code do not move forward to next line while debuging in VS or not, it just hangs forever. What I lack in this code to get list of objects?

Since code hangs in mentioned line issue must be somewhere in HttpClient.

using (HttpClient httpClient = new HttpClient())
            {
                try
                {
                    var headers = httpClient.DefaultRequestHeaders;

                    HttpResponseMessage responseMessage;

                    responseMessage = await httpClient.GetAsync(uri);
                    responseMessage.Content.Headers.ContentType = new HttpMediaTypeHeaderValue("application/x-www-form-urlencoded; charset=UTF-8");

                    var content = responseMessage.Content.ReadAsStringAsync();


                    tvChannelList = JsonConvert.DeserializeObject<List<TvChannels>>(content.GetResults());

                    return tvChannelList;
                }
                catch (Exception ex)
                {
                    throw;
                }
            }

Thanks in advance for any hints.

hal9k2
  • 127
  • 1
  • 11
  • Where are you calling this from? Make sure you are not calling this from the UI thread. – Jon Nov 18 '15 at 21:50
  • Thank you @Jon , I found that I miss `await` operator before calling method...stupid mistake... – hal9k2 Nov 19 '15 at 05:49

2 Answers2

1

Problem was so miserable stupid, I forgot to put await before method name that calls for data from web service, thus blocking UI.

before

ListOfTvChannels = _remoteController.GetChannelListAsync();

after

ListOfTvChannels = await _remoteController.GetChannelListAsync();

Thanks all for trying to help.

hal9k2
  • 127
  • 1
  • 11
0

I use this method when using the HttpClient and want to return an object from a JSON response.

    public async Task<TResult> MakeGetRequest<TResult>(string uri)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
            client.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Charset", "ISO-8859-1");

            HttpResponseMessage response = await client.GetAsync(uri);
            response.EnsureSuccessStatusCode();
            var data = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<TResult>(data);
        }
    }
Shawn C.
  • 6,446
  • 3
  • 34
  • 38
  • I have used your code, but result is same, app hangs in the line of : 'HttpResponseMessage response = await client.GetAsync(uri);' Fiddler shows that server responded and sent data. I have no idea what can be a reason of that. – hal9k2 Nov 18 '15 at 21:27