Xamarin HttpClient 方法 GetAsync 超时错误

我创建了一个 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.



森林海
浏览 260回答 2
2回答

慕码人2483693

考虑使用异步事件处理程序和静态HttpClientstatic HttpClient client = new HttpClient();public MainPage() {&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; loadingData += onLoadingData;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}protected override void OnAppearing() {&nbsp; &nbsp; //loadingData -= onLoadingData; //(optional)&nbsp; &nbsp; loadingData(this, EventArgs.Empty);&nbsp; &nbsp; base.OnAppearing();}private event EventHandler loadingData = delegate { };private async void onLoadingData(object sender, EventArgs args) {&nbsp; &nbsp; var model = await GetPatientData();&nbsp; &nbsp; this.BindingContext = new PatientViewModel(model);}public async Task<PatientModel> GetPatientData() {&nbsp; &nbsp; PatientModel patient = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Uri weburl = new Uri("myuri");&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("a");&nbsp; &nbsp; &nbsp; &nbsp; var response = await client.GetAsync(weburl);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("b");&nbsp; &nbsp; &nbsp; &nbsp; if (response.IsSuccessStatusCode) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("in");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patient = await response.Content.ReadAsAsync<PatientModel>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("in funciton");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }catch(Exception ex) {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(ex);&nbsp; &nbsp; }&nbsp; &nbsp; return patient;}使用这种模式可以帮助避免阻塞调用和套接字耗尽,这有时会导致死锁,从而导致超时。

POPMUISE

尝试这个。public PatientModel abc { get; set; }public MainPage(){&nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; Bridge();&nbsp; &nbsp; // Using abc}public async void Bridge(){&nbsp; &nbsp; abc = new PatientModel();&nbsp; &nbsp; abc = await GetPatientData();}public async Task<PatientModel> GetPatientData(){&nbsp; &nbsp; PatientModel patient = null;&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Uri weburl = new Uri("myuri");&nbsp; &nbsp; &nbsp; &nbsp; HttpClient client = new HttpClient();&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("a");&nbsp; &nbsp; &nbsp; &nbsp; HttpResponseMessage response = await client.GetAsync(weburl);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("b");&nbsp; &nbsp; &nbsp; &nbsp; if (response.IsSuccessStatusCode)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("in");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patient = await response.Content.ReadAsAsync<PatientModel>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("in funciton");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return patient;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return patient;&nbsp; &nbsp; }catch(Exception ex)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(ex);&nbsp; &nbsp; &nbsp; &nbsp; return patient;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP