Xamarin httpclient不会运行多次

时间:2018-02-04 14:28:11

标签: c# xamarin xamarin.forms

我正在使用httpclient通过Xamarin从Api Rest获取数据。我的问题发生在我要在新页面上再次运行httpclient以获取api信息时,也就是说,这只能运行一次,我该如何解决?

代码:

  private async void ObterNoticiasXbox()
        {
            var respXbox = string.Empty;

            try
            {
                var uriXbox = new HttpClient()
                {
                    BaseAddress = new Uri("http://api.newsplay.com.br")
                };
                var urlXbox = "/post/";
                var resultXbox = await uriXbox.GetAsync(urlXbox);


                if (!resultXbox.IsSuccessStatusCode)
                {
                    await DisplayAlert("Erro de Conexão", "Não foi possível obter as notícias do servidor, Tente novamente mais tarde!", "OK");
                    return;
                }

                respXbox = await resultXbox.Content.ReadAsStringAsync();


            }
            catch (Exception ex)
            {
                await DisplayAlert("Erro de Conexão com o Servidor", ex.Message, "OK");
                return;

            }

            // transformando o retorno em objeto através do json e deserealize e retornando em lista
            var NXbox = JsonConvert.DeserializeObject<List<XboxModels>>(respXbox);

            //Adicionando os itens ao ListView na Home.xaml
            XboxList.ItemsSource = NXbox;

        }



    }

0 个答案:

没有答案
相关问题