Xamarin HttpClient 方法 GetAsync 超时错误



我创建了一个 api 来获取数据,但它显示超时错误。我正在调用 Xamarin 主函数中的函数,该函数在应用程序运行时调用。

public MainPage()
    {
        InitializeComponent();
        //this.BindingContext = new PatientViewModel();
        Task<PatientModel> abc = GetPatientData();
    }

我的 api 获取异步调用函数:

public async Task<PatientModel> GetPatientData()
    {
        PatientModel patient = null;
        try
        {
            Uri weburl = new Uri("myuri");
            HttpClient client = new HttpClient();
            Console.WriteLine("a");
            HttpResponseMessage response = await client.GetAsync(weburl);
            Console.WriteLine("b");
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("in");
                patient = await response.Content.ReadAsAsync<PatientModel>();
                Console.WriteLine("in funciton");
                return patient;
            }
            return patient;
        }catch(Exception ex)
        {
            Console.WriteLine(ex);
            return patient;
        }
    }
}

代码未显示任何错误。当执行转到 GetAsync 语句时,它会等待一段时间并发生异常。

System.Net.WebException: The request timed out. ---> Foundation.NSErrorException: Exception of type 'Foundation.NSErrorException' was thrown.

考虑使用异步事件处理程序和静态HttpClient

static HttpClient client = new HttpClient();
public MainPage() {
    InitializeComponent();
    loadingData += onLoadingData;        
}
protected override void OnAppearing() {
    //loadingData -= onLoadingData; //(optional)
    loadingData(this, EventArgs.Empty);
    base.OnAppearing();
}
private event EventHandler loadingData = delegate { };
private async void onLoadingData(object sender, EventArgs args) {
    var model = await GetPatientData();
    this.BindingContext = new PatientViewModel(model);
}
public async Task<PatientModel> GetPatientData() {
    PatientModel patient = null;
    try {
        Uri weburl = new Uri("myuri");
        Console.WriteLine("a");
        var response = await client.GetAsync(weburl);
        Console.WriteLine("b");
        if (response.IsSuccessStatusCode) {
            Console.WriteLine("in");
            patient = await response.Content.ReadAsAsync<PatientModel>();
            Console.WriteLine("in funciton");
        }           
    }catch(Exception ex) {
        Console.WriteLine(ex);
    }
    return patient;
}

使用此模式有助于避免阻塞呼叫和套接字耗尽,这有时会导致死锁,从而导致遇到的超时。

参考异步/等待 - 异步编程的最佳实践

参考 您使用 HttpClient 错误

试试这个。

public PatientModel abc { get; set; }
public MainPage()
{
    InitializeComponent();
    Bridge();
    // Using abc
}
public async void Bridge()
{
    abc = new PatientModel();
    abc = await GetPatientData();
}
public async Task<PatientModel> GetPatientData()
{
    PatientModel patient = null;
    try
    {
        Uri weburl = new Uri("myuri");
        HttpClient client = new HttpClient();
        Console.WriteLine("a");
        HttpResponseMessage response = await client.GetAsync(weburl);
        Console.WriteLine("b");
        if (response.IsSuccessStatusCode)
        {
            Console.WriteLine("in");
            patient = await response.Content.ReadAsAsync<PatientModel>();
            Console.WriteLine("in funciton");
            return patient;
        }
        return patient;
    }catch(Exception ex)
    {
        Console.WriteLine(ex);
        return patient;
    }
}

最新更新