HttpClient非常慢 - Xamarin Forms

时间:2018-05-27 03:00:34

标签: c# xamarin.forms httpclient

我试图从网址下载一些json。 我通过三星设备调试应用程序,但出于某种原因,httpclient需要很长时间才能下载数据。

当我使用TimeSpan.FromMinutes(30)设置超时时,httpclient需要很长时间,这是不切实际的。但是,当我删除超时时,我得到了try catch块捕获的TaskCancelled异常。

之前有没有人见过这种行为?

尝试检查权限:应用程序需要的唯一权限是在调试模式下默认授予的Internet。

任何帮助将不胜感激。谢谢!

修改

这是负责下载数据的类:

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

            }
        }

    }

我从OnStart方法调用LoadFrom方法。

protected override void OnStart ()
        {
            // Handle when your app starts
            API.DataSource.LoadFrom();
        }

2 个答案:

答案 0 :(得分:0)

在华为P9 Lite上测试==&gt;几秒钟才能得到结果......

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)
    {

    }
}

也许你的myclient有一些问题..或者可能是网络问题

答案 1 :(得分:0)

我怀疑您使用的是HttpClientHandler。 Xamarin目前的建议是使用AndroidNativeHandler。这使用本机Android网络堆栈并具有加密支持,而不是在.NET运行时内进行虚拟化。然而,权衡支持仅来自Android 5以后,并且一些HttpClient功能/选项不可用。

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