【问题标题】:Getting NameResolutionFailure error获取 NameResolutionFailure 错误
【发布时间】:2016-06-10 09:27:19
【问题描述】:

无法在 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;
            }

这是我迄今为止尝试过的

  1. 已在 android manifest 中启用 INTERNET
  2. 将主机名转换为 ip
  3. 尝试通过设置client.DefaultRequestHeaders.Host = "intilaqemployees.azurewebsites.net" 直接设置主机;
  4. 在模拟器中关闭 wifi

请注意:Android 模拟器没有任何互联网连接。

【问题讨论】:

  • 你在你的设备上试过了吗?@Shahriar
  • 没有在设备上尝试过。 @AkashAmin
  • 在那里尝试一下,如果没有互联网,您将如何调用服务?
  • PC 有互联网连接,但没有模拟器浏览器。由于我在 VS 中逐步调试代码,我认为在 PC 中连接互联网就足够了,但不确定。
  • 您确定您的 AndroidManifest 中启用了互联网相关权限吗?

标签: xamarin.android httpclient xamarin.forms


【解决方案1】:

这段代码解决了我的问题:

var client = new HttpClient {
    BaseAddress = new Uri("http://1.2.3.4"),
    DefaultRequestHeaders = { Host = "example.com" }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多