第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Xamarin HttpClient 方法 GetAsync 超時(shí)錯(cuò)誤

Xamarin HttpClient 方法 GetAsync 超時(shí)錯(cuò)誤

C#
森林海 2022-07-23 17:44:37
我創(chuàng)建了一個(gè) api 來(lái)獲取數(shù)據(jù),但它顯示超時(shí)錯(cuò)誤。我正在調(diào)用運(yùn)行應(yīng)用程序時(shí)調(diào)用的 Xamarin 主函數(shù)內(nèi)部的函數(shù)。public MainPage()    {        InitializeComponent();        //this.BindingContext = new PatientViewModel();        Task<PatientModel> abc = GetPatientData();    }我的 api GetAsync 調(diào)用函數(shù):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;        }    }}代碼沒(méi)有顯示任何錯(cuò)誤。當(dāng)執(zhí)行到 GetAsync 語(yǔ)句時(shí),它會(huì)等待一段時(shí)間并發(fā)生異常。System.Net.WebException: The request timed out. ---> Foundation.NSErrorException: Exception of type 'Foundation.NSErrorException' was thrown.
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

考慮使用異步事件處理程序和靜態(tài)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;

}

使用這種模式可以幫助避免阻塞調(diào)用和套接字耗盡,這有時(shí)會(huì)導(dǎo)致死鎖,從而導(dǎo)致超時(shí)。


查看完整回答
反對(duì) 回復(fù) 2022-07-23
?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

嘗試這個(gè)。


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;

    }


查看完整回答
反對(duì) 回復(fù) 2022-07-23
  • 2 回答
  • 0 關(guān)注
  • 251 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)