Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Accessing localhost on device and emulator

astahirastahir USMember ✭✭

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.

Answers

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    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.

  • astahirastahir USMember ✭✭

    @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.

  • MihaMarkicMihaMarkic SI ✭✭✭✭
    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.

  • astahirastahir USMember ✭✭

    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

  • MihaMarkicMihaMarkic SI ✭✭✭✭

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

  • astahirastahir USMember ✭✭

    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

  • astahirastahir USMember ✭✭

    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?

  • astahirastahir USMember ✭✭

    @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.

  • astahirastahir USMember ✭✭

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

  • DannyStatenDannyStaten USMember

    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.

  • jacqijvvjacqijvv USMember

    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/

  • MarkZhukovskyMarkZhukovsky USMember ✭✭

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

Sign In or Register to comment.