title: Xamarin App cannot call APIs on LocalHost
body: <p>I am having trouble calling an ASP.NET core API from a Xamarin Android app.
  I am running the android app on an android emulator and I am running the API on my local machine. I have no trouble calling the api from my local machine so I know it works. I am also able to call other apis from Xamarin app. I am using the IP address 10.0.2.2 to route to my local machine. But I have not been able to successfully make any API calls to an API running from my local machine.</p>
  <p>This is my MainPage.xaml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;ContentPage xmlns=&quot;http://xamarin.com/schemas/2014/forms&quot;
               xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
               x:Class=&quot;APICallerTest.MainPage&quot;&gt;
      &lt;StackLayout&gt;
          &lt;Frame BackgroundColor=&quot;#2196F3&quot; Padding=&quot;24&quot; CornerRadius=&quot;0&quot;&gt;
              &lt;Label Text=&quot;API Caller Test&quot; HorizontalTextAlignment=&quot;Center&quot; TextColor=&quot;White&quot; FontSize=&quot;36&quot;/&gt;
          &lt;/Frame&gt;
          &lt;StackLayout Padding=&quot;30,24,30,0&quot;&gt;
              &lt;Button x:Name=&quot;CallAPIButton&quot; VerticalOptions=&quot;Center&quot; Text=&quot;Call API&quot; Clicked=&quot;CallAPIButton_Clicked&quot;/&gt;
              &lt;Label x:Name=&quot;CalledLabel&quot;&gt;&lt;/Label&gt;
              &lt;Label x:Name=&quot;ResultLabel&quot;&gt;&lt;/Label&gt;
              &lt;Label x:Name=&quot;ErrorLabel&quot;&gt;&lt;/Label&gt;
              &lt;Label x:Name=&quot;StackTraceLabel&quot;&gt;&lt;/Label&gt;
          &lt;/StackLayout&gt;
      &lt;/StackLayout&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  <p>This is my MainPage.xaml.cs:</p>
  <pre><code>using System;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Xamarin.Forms;
  
  namespace APICallerTest
  {
      public partial class MainPage : ContentPage
      {
          public MainPage()
          {
              InitializeComponent();
          }
          private async Task CallAPI()
          {
              ResultLabel.Text = &quot;&quot;;
              ErrorLabel.Text = &quot;&quot;;
              StackTraceLabel.Text = &quot;&quot;;
  
              try
              {
                  string uri = &quot;http://10.0.2.2:57817/Weatherforecast&quot;;
  
                  HttpClient httpClient = new HttpClient();
                  HttpResponseMessage httpResponse = await httpClient.GetAsync(uri);
  
                  if (httpResponse.IsSuccessStatusCode)
                  {
                      string result = await httpResponse.Content.ReadAsStringAsync();
                      ResultLabel.Text = $&quot;Result: {result}&quot;;
                  }
                  else
                  {
                      ResultLabel.Text = &quot;Result: failed&quot;;
                  }
              }
              catch (Exception ex)
              {
                  ErrorLabel.Text = $&quot;Error Message: {ex.Message}&quot;;
                  StackTraceLabel.Text = $&quot;Stack Trace: {ex.StackTrace}&quot;;
              }
          }
  
          private void CallAPIButton_Clicked(object sender, EventArgs e)
          {
              CalledLabel.Text = &quot;Called&quot;;
              CallAPI();
          }
      }
  }
  </code></pre>
  <p>This is what my api looks like on postman:</p>
  <p><a href="https://i.stack.imgur.com/CXGKM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CXGKM.png" alt="enter image description here" /></a></p>