Xamarin app unable to fetch api data from emulator, works well on a physical device

My app wouldn't fetch data in emulator. At first I thought it's the whole emulator but other apps like youtube and chrome can access the internet just fine. I'm new to xamarin and c# so any help will be appreciated.

1 answer

  • answered 2018-11-12 17:35 Nick Kovalsky

    Talking about Android emulator, 2 things to set-up:

    1. You would use http://10.0.2.2:55858 instead of http://localhost:55858. Protocol and port number are here just for pure example, they depend on your server app settings.

    2. Check if your modified address opens in chrome inside emulator. If you got Bad Gateway and such, proceed to the fun part:

      • locate your solution directory, once inside look for subdir ".vs/config".
      • open "applicationhost.config" in some text editor, look for your address string like "localhost:55858".
      • lets assume you found something like:

        <bindings>
            <binding protocol="http" bindingInformation="*:55858:localhost" />
            <binding protocol="https" bindingInformation="*:44337:localhost" />
        </bindings>
        

    now add lines here to obtain the result:

            <bindings>
                <binding protocol="http" bindingInformation="*:55858:localhost" />
                <binding protocol="https" bindingInformation="*:44337:localhost" />
                <binding protocol="http" bindingInformation="*:55858:127.0.0.1" />
                <binding protocol="https" bindingInformation="*:44337:127.0.0.1" />
            </bindings>
    

    this way you ask your IIS express to redirect external requests to your machine

    • close visual studio
    • find the running IIS (if any) icon in your task bar near the clock, right-click it and close
    • now you can run everything again, it will pick up the modified "applicationhost.config"