2
votes

I'm trying to download some json from a url. I'm debugging the app through my Samsung device, but for some reason, httpclient takes very long to download the data.

When I set the timeout using TimeSpan.FromMinutes(30), httpclient takes very long which is not practicable. When I remove the timeout, however, I get TaskCancelled exception which the try catch block catches.

Has anyone seen this behavior before?

Tried checking the permissions: The only permission the app needs is the internet which by default is granted in the debug mode.

Any help would be appreciated. Thanks!


Edit

This is the class responsible for downloading the data:

 public static class DataSource
    {
        public async static void LoadFrom()
        {
               var uri = new Uri("https://api.coinmarketcap.com/v2/ticker/?convert=usd&sort=price");    
            try
            {
                bool isConnected = CrossConnectivity.Current.IsConnected;
                HttpClient myClient = new HttpClient(new Xamarin.Android.Net.AndroidClientHandler());
                var response = await myClient.GetAsync(uri);
                if (response.IsSuccessStatusCode)
                {
                    var Data = await response.Content.ReadAsStringAsync();
                    var CoinMarketCapObject = JsonConvert.DeserializeObject<CoinMarketCap.CoinMarketCapCurrencyData>(Data);
                    List<Currency> currencies = new List<Currency>();
                    if (CoinMarketCapObject != null)
                    {
                        foreach (var Datum in CoinMarketCapObject.Data)
                        {
                            currencies.Add(new Currency(Datum.Value.name, Datum.Value.symbol, Datum.Value.quotes.USD.price));
                        }
                    }
                }
            }
            catch (TimeoutException ex)
            {
                // Check ex.CancellationToken.IsCancellationRequested here.
                // If false, it's pretty safe to assume it was a timeout.
            }
            catch (TaskCanceledException ex)
            {
                // Check ex.CancellationToken.IsCancellationRequested here.
                // If false, it's pretty safe to assume it was a timeout.
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);

            }
        }

    }

I'm calling the LoadFrom method from the OnStart Method.

protected override void OnStart ()
        {
            // Handle when your app starts
            API.DataSource.LoadFrom();
        }
2
How and where is that method being calling? - Nkosi
Does data actually return? Are you sure you are not in a deadlock? - Nkosi
@Nkosi I have added some info... - Amen Jlili
If you download the data using a web browser, how long does it take to download? - mjwills
Thw answer probably is in how you call this Load() method, Post the whole chain, from the top method (eventhandler) on down. - Henk Holterman

2 Answers

1
votes

I suspect you are using the HttpClientHandler. The current recommendation from Xamarin is to use the AndroidNativeHandler. This uses the native Android networking stack and has encryption support instead of being virtualised within the .NET runtime. However, the trade-offs are support is only from Android 5 onwards, and some HttpClient features/options are not available.

https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack

0
votes

Tested on Huawei P9 Lite ==> few seconds to get the result...

public async void LoadFrom()
{
    var uri = new Uri("https://api.coinmarketcap.com/v2/ticker/?convert=usd&sort=price");
    try
    {
        HttpClient myClient = new HttpClient();
        var response = await myClient.GetStringAsync(uri);
        Console.WriteLine(response);
    }
    catch (TimeoutException ex)
    {
        // Check ex.CancellationToken.IsCancellationRequested here.
        // If false, it's pretty safe to assume it was a timeout.
    }
    catch (TaskCanceledException ex)
    {
        // Check ex.CancellationToken.IsCancellationRequested here.
        // If false, it's pretty safe to assume it was a timeout.
    }
    catch (Exception e)
    {

    }
}

Perhaps your myclient has some issues.. or maybe a network problem