亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 248 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號