Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

HTTPClient hangs at GetAsync when debugging on Android device

$
0
0

I'm getting some strange behavior with my HTTPClient when running on a device. I originally created a test C# console project just to test out the results from our server API, and everything went fine. But as soon as I brought the same code into my Xamarin PCL, the app hangs as soon as it reaches the GetAsync method and tries to access the internet. Here is the code that worked with a C# console project:

public async Task<List<User>> getUsers(string controller, string action, string username)
        {
            var client = new HttpClient();

            var response = await client.GetAsync(address + controller + action + username); // THIS IS WHERE THE CODE HANGS ON THE DEVICE

            var userJson = response.Content.ReadAsStringAsync().Result;

            var rootObject = JsonConvert.DeserializeObject<Model.userRoot>(userJson);

            return rootObject.data;
        }

Then in my ContentPage I just have a simple foreach loop that grabs each entry and adds them to my List:
myclient TEST_CLIENT = new myClient(username);
var TEST_USER_LIST = TEST_CLIENT.getUsers(controller, action); // HANGS HERE BECAUSE OF PREVIOUS GetAsync CALL

            foreach (User u in TEST_USER_LIST.Result)
            {
                var entry = new User()
                {
                    userName = u.firstName + " " + u.lastName
                };
                userList.Add(entry);
            }

Like I mentioned, all of this worked in a console project so I'm not sure why it wouldn't here. It isn't an issue with referencing libraries since I'm able to compile and run the app itself just fine, it's just when it gets to this point that it breaks. I made sure that my device is connected to the internet as well. (Also, I haven't tried on iOS yet because I only have access to Android devices at the moment)


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>