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();
}