Android emulator not connecting to localhost api

11,290

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

    enter image description here

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

    enter image description here

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

    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 /, 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.

Share:
11,290
Dönci Fekete
Author by

Dönci Fekete

Updated on July 06, 2022

Comments

  • Dönci Fekete
    Dönci Fekete almost 2 years

    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)

  • Dönci Fekete
    Dönci Fekete over 5 years
    Yes, It is true! I was debugging it. My android emulator use 10.0.2.2 adress. But, how to add a new incoming rule for the program or port ?
  • Rizwan Ahmed Shivalli
    Rizwan Ahmed Shivalli over 5 years
    @DönciFekete Turn on your local server (WAMP, XAMP), Open Emulator and type 169.254.80.80 in the chrome or any browser in emulator and check if the wamp/ xamp page is showing or not. if it is not showing tell the error code what it is showing.
  • Rizwan Ahmed Shivalli
    Rizwan Ahmed Shivalli over 5 years
    if you are using latest wamp server then follow steps at this link betterhostreview.com/access-wamp-server-local-network.html
  • Dönci Fekete
    Dönci Fekete over 5 years
    I use RESTFull web service like in this example developer.xamarin.com/samples/xamarin-forms/WebServices/…. So, I do not follow your instructions. Do you have any recommendations?
  • Rizwan Ahmed Shivalli
    Rizwan Ahmed Shivalli over 5 years
    If you are using local server to perform add, delete, edit etc operations then please follow the above steps if you are using an online server then check if you have given Internet permissions to your app
  • Dönci Fekete
    Dönci Fekete over 5 years
    Oh man. Yes, It is working! Thanks a lot! ( I have never seen anything like this before)
  • neena
    neena over 4 years
    Even this is not helping me in flutter android and dot net server
  • Paul Pérez
    Paul Pérez about 4 years
    @neena I followed all steps and its working in Flutter, using IIS Express (API developed in .NET Core).
  • Savage
    Savage about 3 years
    Nice tool - just keep in mind the free version only allows sessions of 2 hours each, and the url they generate changes every time, which is pretty annoying