为了账号安全,请及时绑定邮箱和手机立即绑定

Xamarin HttpClient 方法 GetAsync 超时错误

Xamarin HttpClient 方法 GetAsync 超时错误

C#
森林海 2022-07-23 17:44:37
我创建了一个 api 来获取数据,但它显示超时错误。我正在调用运行应用程序时调用的 Xamarin 主函数内部的函数。public MainPage()    {        InitializeComponent();        //this.BindingContext = new PatientViewModel();        Task<PatientModel> abc = GetPatientData();    }我的 api GetAsync 调用函数: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.
查看完整描述

2 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

考虑使用异步事件处理程序和静态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;

}

使用这种模式可以帮助避免阻塞调用和套接字耗尽,这有时会导致死锁,从而导致超时。


查看完整回答
反对 回复 2022-07-23
?
POPMUISE

TA贡献1765条经验 获得超5个赞

尝试这个。


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;

    }


查看完整回答
反对 回复 2022-07-23
  • 2 回答
  • 0 关注
  • 259 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号