简体   繁体   中英

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)

There is a simple workaround to connect Emulator & Simulator to localhost API's which I am using.

Download third party software NgRok for Windows at any specific location of your PC. It just contains one executable file called ngrok.exe. Now execute your service on your preferred browser. Than follow below steps.

  • Open your ngrok location on command prompt

    在此处输入图片说明

  • Open your service URL(take from your browser) with prefix: ngrok http --host-header=rewrite

    在此处输入图片说明

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

    在此处输入图片说明

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

Benifits

  1. By using this method 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.

That 10.0.2.2 address is used for the Google emulators. For the Microsoft emulator you want to use 169.254.80.80. If that doesn't work then it must mean you have the firewall on and it's blocking the traffic. That can be solved by adding a new incoming rule for the program or port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM