4

I'm running a service through ASP.net/Visual Studio that's being developed in conjunction with an Android app. When I run the service through VS, it's accessible at http://localhost:13980/ but not http://127.0.0.1:13980/ (which gives a "bad request - invalid hostname" 400 error). Obviously, I can't tell the Android app to look for a service at "localhost" since that'll be pointing at itself.

I understand (according to this page) that the Android emulator treats 10.0.2.2 as a pass-through IP address to the host machine's 127.0.0.1, so it would stand to reason that everything would come together if I a) got localhost to be properly exposed through 127.0.0.1, and b) changed the target address in the app from localhost:port to 10.0.2.2:port.

How would I go about doing part A? I'm running Windows 8.1 Pro, Visual Studio 2012, and the project in question is targeting .NET 4.5.

Thanks!

Community
  • 1
  • 1
Benjin
  • 2,264
  • 2
  • 25
  • 50

3 Answers3

0

You have two options:

  1. Run the web service under IIS instead of VS Cassini. This way you can bind the web service to your local network's IP address, e.g. http://10.0.2.2:8081
  2. Modify the hosts file on your Android emulator to redirect localhost to your computer's IP address

The second one isn't a great solution. It may cause some conflicts with the OS and I'm not even sure Cassini would still even serve the request. Visual Studio's in-built web server (Cassini) only listens to local requests, so option (1) really is the most flexible. Plus, since you have W8 Pro IIS is already built-in.

CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • Can you point me to a resource that would walk me through doing that? What I'm envisioning right now would require manual redeployment every time I update the server rather than Cassini's "hit F5 and it's going" approach, which would suck. Is there some some sort of configuration I can do to have it be a one-click thing? Also, would #2 actually work if the problem #1's addressing isn't resolved? – Benjin Nov 19 '13 at 12:44
  • Could I make the change in the app to 10.0.2.2:8081 (ps: where'd that port come from? my service is running on :13980) and then change my computer's hosts file to redirect 127.0.0.1 to localhost, or does it not work like that? – Benjin Nov 19 '13 at 12:49
  • You can set visual studio up so that when you press F5, it compiles and launches IIS instead. Here is a good step-by-step guide: http://idosql.com/2011/05/25/181/ - Can't put one here, it would be too long! – CodingIntrigue Nov 19 '13 at 14:39
0

The IP address 10.0.2.2 IP maps to the the 127.0.0.1 IP address, not to localhost.

From the emulator, connect to 10.0.2.2, and then host your site on 127.0.0.1 instead of localhost. That should work.

Cobus Kruger
  • 8,338
  • 3
  • 61
  • 106
-1

Use the IP address 10.0.2.2 instead. See this: How to connect to my http://localhost web server from Android Emulator in Eclipse

Community
  • 1
  • 1
Oscar
  • 13,594
  • 8
  • 47
  • 75