M
M
MaxFloat2020-07-14 07:41:59
Android
MaxFloat, 2020-07-14 07:41:59

Xamarin+Android: Why does SendAsync hang when debugging on a device?

I just started trying Xamarin, no experience. To be debugged, for technical reasons, I can only on the device. I wrote a simple code to connect to my site, but SendAsync keeps freezing. Tried Post and Get requests - the effect is the same. At the same time, the browser normally goes to this address, Postman - too. I double-checked the address a hundred times, there is Internet on the device. In the manifest, I checked the boxes for INTERNET, READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE. It would be clear if there was a response like 404 or 500, but in general a hang - why? Tell me what else to check...

HttpRequestMessage request = new HttpRequestMessage();
            request.RequestUri = new Uri("https://<my_server_name>");
            HttpClient client = new HttpClient();
            request.Method = HttpMethod.Get;
            using (HttpResponseMessage response = await client.SendAsync(request))
            {
                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                    Toast.MakeText(Application.Context, result, ToastLength.Long);
                }
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxFloat, 2020-07-20
@MaxFloat

I figured it out myself. You can't do this on the UI thread. Wrapped request in AsyncTask - earned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question