HttpClient freezing on Android

Guy 96 Reputation points
2020-11-29T17:10:06.66+00:00

I have a very simple MVVM application using Xamrin Forms and Prism, built from the Prism Xamarin Template.

When I initialize a new ViewModel I am trying to make a call to an API, on UWP this is working without any issues but on Android it is just freezing and not returning any errors or throwing any exceptions.

I have simplified the code down to where the error is happening and tested with a URI that anyone can use.

public async Task InitializeAsync(INavigationParameters parameters)  
    {  
        try  
        {  
            var client = new HttpClient();  
              
            var response = await client.GetAsync("https://www.google.co.uk").ConfigureAwait(false);  
        }  
        catch (Exception ex)  
        {  
            System.Diagnostics.Debug.WriteLine(ex.Message);  
        }  
    }  

I am using the below NuGets

  • Prism.Core v8.0.0.1909
  • Prism.Dryloc.Forms v8.0.0.1909
  • Xamarin.Forms v4.8.0.1687

I have set the HttpClient implementation to Android and Native TLS 1.2+ enter image description here
43270-image.png

I have set internet permissions in the Android Manifest
43478-image.png

I have reviewed several other posts that seem similar but have had no joy in fixing the issue, can anyone shed any light on how I can troubleshoot this or get it working?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,298 questions
0 comments No comments
{count} votes

Accepted answer
  1. Guy 96 Reputation points
    2020-12-01T13:44:48.68+00:00

    After troubleshooting this for a while and banging my head against a brick wall I've finally figured out what was going wrong.

    Nothing!

    Step through on Android wasn't stepping past line 7 below and the screen wasn't redrawing.

    var response = await client.GetAsync("https://www.google.co.uk").ConfigureAwait(false);
    

    When I turned my phone 90 degrees the screen would redraw and I could see that the request had worked.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2020-11-29T17:24:44.503+00:00

    Have you set internet permission in the manifest?