[英]HttpClient is extremely very slow - Xamarin Forms
I'm trying to download some json from a url. 我正在尝试从URL下载一些json。 I'm debugging the app through my Samsung device, but for some reason, httpclient takes very long to download the data.
我正在通过三星设备调试该应用程序,但是由于某些原因,httpclient需要很长时间才能下载数据。
When I set the timeout using TimeSpan.FromMinutes(30), httpclient takes very long which is not practicable. 当我使用TimeSpan.FromMinutes(30)设置超时时,httpclient花费的时间很长,这是不可行的。 When I remove the timeout, however, I get TaskCancelled exception which the try catch block catches.
但是,当我删除超时时,会收到try catch块捕获的TaskCancelled异常。
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. 尝试检查权限:应用程序所需的唯一权限是Internet,默认情况下,Internet是在调试模式下授予的。
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. 我正在从OnStart方法调用LoadFrom方法。
protected override void OnStart ()
{
// Handle when your app starts
API.DataSource.LoadFrom();
}
Tested on Huawei P9 Lite ==> few seconds to get the result... 在华为P9 Lite上测试==>几秒钟即可得到结果...
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 也许您的myclient遇到了一些问题..或网络有问题
I suspect you are using the HttpClientHandler
. 我怀疑您正在使用
HttpClientHandler
。 The current recommendation from Xamarin is to use the AndroidNativeHandler
. Xamarin当前的建议是使用
AndroidNativeHandler
。 This uses the native Android networking stack and has encryption support instead of being virtualised within the .NET runtime. 它使用本机Android网络堆栈并具有加密支持,而不是在.NET运行时中进行虚拟化。 However, the trade-offs are support is only from Android 5 onwards, and some HttpClient features/options are not available.
但是,权衡是仅从Android 5开始才支持,并且某些HttpClient功能/选项不可用。
https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack https://docs.microsoft.com/zh-cn/xamarin/android/app-fundamentals/http-stack
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.