我正在开发和使用 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 权限。我不知道我是否遗漏了什么,是否在调试我的应用程序或其他任何东西