我在本地主机上运行了一个简单的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,但我遇到了麻烦。
我已将Url更改为:
client.BaseAddress = new Uri("http://10.0.2.2:XXXX)根据我的理解,这应该允许模拟器调用localhost。
发生的情况是连接超时。如果我把BP放在这条线上:
HttpResponseMessage response = client.GetAsync("api/users/").Result;Unknown member说,将鼠标悬停在GetAsync上,感觉可能与此有关?有什么建议吗?
发布于 2015-08-26 12:57:28
请参阅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。
https://stackoverflow.com/questions/28851866
复制相似问题