3

I have a simple web-api running on localhost. This is how I call it:

 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;

            }

Now I would like to call the API from my android emulator but Im unning into trouble.

I have changed the Url to:

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

This should allow the emulator to call the localhost from what I have understood.

What happens is that the connection Times-out. If I put a BP on the line:

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

And hover over GetAsync, it says Unknown member, feels like that may have something to do with it? Any tips?

bugsy
  • 111
  • 2
  • 13
  • 1
    Have you tried doing the same from actual device? See: http://stackoverflow.com/questions/6192726/android-emulator-loopback-to-iis-express-does-not-work-but-does-work-with-cassi – Milen Mar 04 '15 at 14:03
  • Thank you for answering. I tried to foollow the links instructions and added: to my IIS-express-config. Same result... – bugsy Mar 04 '15 at 14:25
  • I had a similar issue but it was working fine from real device... – Milen Mar 04 '15 at 14:30
  • Aha, maybe I missunderstodd. I do not have a real device to try it on. aybe it would work...thank you – bugsy Mar 04 '15 at 14:33
  • 1
    Make sure that the network is available on the emulator. It's also possible that you have some firewall settings that may be blocking the emulator from making the connection to the local host. It's hard to say without actually debugging it, but those are some things to try. – SharpMobileCode Mar 04 '15 at 20:31
  • Thank you for answering. I got it to work by using the IP-adress oinstead of 10.0.2.2. – bugsy Mar 05 '15 at 18:51

1 Answers1

0

Please see the official documentation for 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

In you should try 10.0.2.2.

der_michael
  • 3,151
  • 1
  • 24
  • 43