无法在Xamarin.Forms项目中获取数据。我尝试使用以下代码并且出现 NameResolutionFailure 错误。
private const string BaseUrl = "http://intilaqemployees.azurewebsites.net/api/employeesapi";
public async Task<List<Employee>> GetEmployeesAsync()
{
var httpClient = new HttpClient();
try
{
var jsonResponse = await httpClient.GetStringAsync(BaseUrl).ConfigureAwait(false);
//The following line never gets executed
var employeesList = JsonConvert.DeserializeObject<List<Employee>>(jsonResponse);
return employeesList;
}
catch (AggregateException exception) { }
catch (Exception ex)
{
}
return null;
}
这是我到目前为止所尝试的
请注意:Android模拟器没有任何互联网连接。
答案 0 :(得分:1)
这个代码解决了我的问题:
var client = new HttpClient {
BaseAddress = new Uri("http://1.2.3.4"),
DefaultRequestHeaders = { Host = "example.com" }
};