【问题标题】:Call web-Api with xamarin使用 xamarin 调用 web-Api
【发布时间】:2015-05-05 06:55:24
【问题描述】:

我有一个在 localhost 上运行的简单 web-api。我是这样称呼它的:

 using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:XXXXX/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = client.GetAsync("api/users/").Result;

            }

现在我想从我的 android 模拟器调用 API,但我遇到了麻烦。

我已将网址更改为:

client.BaseAddress = new Uri("http://10.0.2.2:XXXX)

这应该允许模拟器根据我的理解调用本地主机。

发生的情况是连接超时。如果我把BP放在线上:

HttpResponseMessage response = client.GetAsync("api/users/").Result;

然后将鼠标悬停在GetAsync 上,上面写着Unknown member,感觉这可能与它有关?有什么建议吗?

【问题讨论】:

  • 你试过在实际设备上做同样的事情吗?见:stackoverflow.com/questions/6192726/…
  • 感谢您的回答。我试图愚弄链接说明并添加: 到我的 IIS-express-config。结果一样...
  • 我遇到了类似的问题,但在真实设备上运行良好...
  • 啊哈,也许我想念。我没有真正的设备来试用它。也许它会工作......谢谢你
  • 确保网络在模拟器上可用。您也可能有一些防火墙设置可能会阻止模拟器与本地主机建立连接。如果不实际调试它很难说,但这些都是可以尝试的。

标签: xamarin


【解决方案1】:

请查看Using the Emulator的官方文档:

Network Address  Description
10.0.2.1         Router/gateway address
10.0.2.2         Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
10.0.2.3         First DNS server
10.0.2.4 / 10.0.2.5 / 10.0.2.6  Optional second, third and fourth DNS server (if any)
10.0.2.15        The emulated device's own network/ethernet interface
127.0.0.1        The emulated device's own loopback interface

你应该试试10.0.2.2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2018-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多