0

我正在开发和使用 API 的应用程序(使用 genymotion 模拟器进行测试),我已经制作了一个简单的测试内容页面,其中包含以下代码:

using PlaqueoApp.Modelos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Net.NetworkInformation;
using Newtonsoft.Json;

namespace PlaqueoApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RegistrarPatron : ContentPage
    {
        public RegistrarPatron ()
        {
            InitializeComponent ();

            GetOficinas().Wait();

        }

        private async Task GetOficinas()
        {
            Ping myPing = new Ping();
            PingReply reply = myPing.Send("8.8.8.8", 10000);


            HttpClient cliente = new HttpClient();

            string url = "https://reqres.in/api/users?page=2";

            var response = await cliente.GetStringAsync(url);

            var anonimus = JsonConvert.DeserializeObject<List<object>>(response);


        }
    }
}

我的问题是,当它到达 GetStringAsync 时,它会永远持续下去,我的意思是,方法调用永远不会返回,我必须停止它。响应应该是这样的:

{
  "page": 2,
  "per_page": 3,
  "total": 12,
  "total_pages": 4,
  "data": [
    {
      "id": 4,
      "email": "eve.holt@reqres.in",
      "first_name": "Eve",
      "last_name": "Holt",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
    },
    {
      "id": 5,
      "email": "charles.morris@reqres.in",
      "first_name": "Charles",
      "last_name": "Morris",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
    },
    {
      "id": 6,
      "email": "tracey.ramos@reqres.in",
      "first_name": "Tracey",
      "last_name": "Ramos",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
    }
  ]
} 

我以为是因为 Iternet 连接,但我检查了 Gennymotion 模拟器,并且启用了 WIFI 选项。如您所见,我还对谷歌进行了 ping 操作,我获得了成功状态,我还在清单上添加了 Internet 权限。我不知道我是否遗漏了什么,是否在调试我的应用程序或其他任何东西

4

2 回答 2

2

不要这样做:

        public RegistrarPatron ()
        {
            InitializeComponent ();

            GetOficinas().Wait();
        }

你几乎不应该在这样的任务上等待():它会冻结你的应用程序或永远阻塞一个线程。

改为这样做:

namespace PlaqueoApp
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class RegistrarPatron : ContentPage
    {
        public RegistrarPatron ()
        {
            InitializeComponent ();
        }

        protected override async void OnAppearing()
        {
            try 
            {
                await GetOficinas();
            }
            catch (Exception exception)
            {
                Debug.WriteLine(e.ToString());
            }   
        }

        private async Task GetOficinas()
        {
            Ping myPing = new Ping();
            PingReply reply = myPing.Send("8.8.8.8", 10000);

            HttpClient cliente = new HttpClient();

            string url = "https://reqres.in/api/users?page=2";

            var response = await cliente.GetStringAsync(url);

            var anonimus = JsonConvert.DeserializeObject<List<object>>(response);
        }
    }
}
于 2019-08-13T19:53:16.800 回答
0

HttpClient在发出请求之前修改命名的超时并cliente在请求周围添加一个try/catch块以查看发生了什么。

如果您有一个单独的异步方法进行调用,您可以将 try/catch 放在那里,然后调用单独的方法。否则,.Wait()在围绕它进行异常处理之前调用该任务。这Wait()将用于测试目的而不是生产用途。

于 2019-08-13T17:26:30.790 回答