locked
Accessing localhost on device and emulator RRS feed

  • Question

  • User13328 posted

    Hello there,

    I worked on an app a couple of years ago and now I have to make enhancements to it. As it was my first (and only so far!) project so I had problem accessing web service on device as well as emulator at the beginning. However, I finally managed to run on emulator when I asked the following question on this forum: https://forums.xamarin.com/discussion/5324/web-service-creation-and-consumption-in-an-android-app

    Now the problem is that when I tried running the same code, it does not access localhost through emulator. I never managed to run it on device even earlier and now too. I was using 10.0.2.2 for emulator. Here are the points on which I would like your help please:

    1) Why am I not able to run the same exact code to get to localhost? I can access the web service when I open it in my laptop's browser like http://localhost:16299/MainService/PerformAnAction?str1=test. As I already said the code is exactly the same, one difference is that I previously worked on Windows 7 and now it's Windows 8. Is there any setting to be modified? I tried applying few things which I found on the net but I either get Request TimeOut or Error: ConnectFailure (Connection timed out) ... I guess both basically tell time out.

    2) I would really appreciate if you can guide me on how to access localhost through an actual device because emulators are usually very slow. I once again tried running it on an actual device through the help of the internet but here again I get one of the two errors mentioned in the previous point.

    3) Can you also suggest some faster and possibly lightweight emulators? Though I have 8GB RAM but AVD as well as Xamarin Android Player both take so much time (and often eventually crash) as if I'm trying to run them on a 2GB machine!

    Thank you.

    Monday, May 11, 2015 2:07 PM

All replies

  • User112 posted

    Localhosts are accessible only to local machine. Thus it works on laptop since you are running it on laptop but it doesn't on emulator or device, since you aren't running it there. One way is to open your service (IISExpress needs some config editing, firewall has to be configured properly) and then access it using either IP or host name. As per emulators, the Android best one is Genymotion.

    Tuesday, May 12, 2015 7:03 AM
  • User13328 posted

    @MihaMarkic - thanks for the response.

    1) I have tried following the link for IISExpress configuration but I did not find a folder at %userprofile%\documents\iisexpress\ ... so not sure what to do next!

    2) I had already allowed inbound as well as outbound connections to my port 16299 for Windows Firewall but to no avail.

    3) I will download and install Genymotion now and let you know how did it go.

    By the way, I would prefer if you can tell me how to access localhost from actual device connected via USB to my laptop as I have the device with me.

    Tuesday, May 12, 2015 11:11 AM
  • User112 posted
    1. Are you using IIS Express in first place? Or IIS? Something else?
    2. Test from a browser from another (virtual) machine.

    Accessing it should be done as devices on same network. AFAIK there is no direct way other than through TCP/IP.

    Tuesday, May 12, 2015 11:15 AM
  • User13328 posted

    Sorry, the application gets hosted to ASP.NET Development Server when I run it through Visual Studio 2010. Can you tell me what should I do?

    Thanks

    Tuesday, May 12, 2015 11:26 AM
  • User112 posted

    First make sure that the dev server and your app are accessible from outside your machine. A browser is a good starting point.

    Tuesday, May 12, 2015 11:59 AM
  • User13328 posted

    Thanks for getting back Miha.

    When I open the following link on my laptop, it works fine: http://localhost:16299/MainService/PerformSomething?username=dummy

    Obviously there is no localhost for emulator, so I wrote this in the browser of emulator but I get "webpage not available" with an additional message saying 'request timed out': http://10.0.2.2:16299/MainService/PerformSomething?username=dummy

    Tuesday, May 12, 2015 12:16 PM
  • User13328 posted

    Well got the web service accessible on my Genymotion emulator with the help of this: http://stackoverflow.com/questions/18788805/genymotion-which-ip-set-in-etc-hosts-file-for-access-hosts-local-webserver-vi

    So I used 10.0.3.2 instead of 10.0.2.2.

    However the problem now is that I don't see my emulator through Xamarin Studio; what do I need to do to make it possible?

    Tuesday, May 12, 2015 12:21 PM
  • User13328 posted

    @MihaMarkic - I now see the emulator in the list while I start debugging from Xamarin Studio and I can also access the web service on Genymotion emulator. In other words, I can resume my work via emulator. However I still remain stuck if I wish to use an actual device instead of the emulator. Can you (or anybody else on this forum) guide me on how to achieve that?

    By the way, the problem was that the emulator needed to be connected using "abd connect #.#.#.#" command. If anybody faces a similar issue in future, please go to the following link and see the complete discussion to get the whole picture: http://www.codenutz.com/xamarin-for-android-genymotion/#comment-2020347659

    Thanks.

    Tuesday, May 12, 2015 7:23 PM
  • User13328 posted

    Dear Xamarin enthusiasts, I still await your guidance...

    Monday, May 18, 2015 1:53 PM
  • User270538 posted

    Plus one on this. I have the means to get my services to a publicly visible end point, but I don't have access in my organization to update that end point regularly or to debug on that server. It certainly appears that there is a lot of difficult set up required for this. In my ideal world, there would be a simple way in a Xamarin project to add some configurations to the emulators. I know that is asking a whole lot, but it would sure be awesome.

    Monday, October 31, 2016 6:01 PM
  • User166461 posted

    Hi all, I've written a blog post on this. It is really simple to do this using SharpProxy. The link to my article:

    https://www.barelycompetent.co.za/debugging-your-rest-service-from-your-emulator-device-using-sharpproxy/

    Sunday, April 2, 2017 6:29 AM
  • User272075 posted

    @astahir that right there is your answer, great stuff @jacqijvv !

    Saturday, November 4, 2017 4:36 PM
  • User392062 posted

    Try using this extension for Visual Studio - https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

    Wednesday, July 1, 2020 9:26 AM