Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android emulator not connecting to localhost api

I have a Xamarin app and localhost API. I'm trying to connect to the API from the app, but it could not be found. I edited my applicationhost.config file and I tried localhost, 10.0.2.2 and my IP address in the call, but it was not working.

So, how do I configure simple API access? (The API itself is working well)

like image 906
Dönci Fekete Avatar asked Oct 03 '18 01:10

Dönci Fekete


People also ask

What is the localhost for Android emulator?

As you've learned, when you use the emulator, localhost ( 127.0. 0.1 ) refers to the device's own loopback service, not the one on your machine as you may expect. You can use 10.0. 2.2 to access your actual machine, it is an alias set up to help in development.

How do I fix Android emulator not working?

If the Android Emulator does not start properly, this problem is often caused by problems with HAXM. HAXM issues are often the result of conflicts with other virtualization technologies, incorrect settings, or an out-of-date HAXM driver. Try reinstalling the HAXM driver, using the steps detailed in Installing HAXM.

How do I access localhost API from my Android to run an Android app against a localhost API?

Start your server at localhost and attach the debugger. Next, change the API endpoints in your Android code to http://10.0.2.2 . This reroutes the requests from your emulator to your computer's localhost. Run the Android app on the emulator and cause the requests you want to debug.

Why is my AVD not working?

Android Virtual Devices fail to launch on ChromeOS On ChromeOS, Android Virtual Devices (AVDs) might fail to launch because the libnss3 dependency is missing. To launch the AVDs successfully, run sudo apt install libnss3 to manually install the libnss3 library.


1 Answers

There is simple workaround to connect Emulator & Simulator to localhost API.

Download third party software NgRok for Windows at any specific location of your PC. It just contains one executable file called ngrok.exe (you not need to install it).

Now execute your service on your preferred browser. Than follow below steps.

  • Open your ngrok.exe file path in command prompt

enter image description here

  • Keep your service url (take from your browser) with prefix ngrok http --host-header=rewrite and hit enter.

enter image description here

  • Now go to your browser where you have executed your service open new tab and type localhost:4040 than press enter -->Go to status there you will find your public url which you can use in your mobile application.

enter image description here

Note: Url structure should be like this public const string BaseUri = "https://8c56892f.ngrok.io/"; followed by / in last. Try not to append unnecessary /.

Benifits

  1. You can debug services from one Visual Studio to another.

  2. Your url will be active until you close command prompt.

  3. In browser or command prompt track your requests status like Ok, Not found etc.

For more information visit this https://www.c-sharpcorner.com/article/exposing-local-web-server-to-internet-using-ngrok/

Hope it help you.

like image 52
R15 Avatar answered Oct 19 '22 08:10

R15