Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Xamarin Forms API - Can't Connect

Xamarin Forms API - Can't Connect

Scheduled Pinned Locked Moved Android
comdebuggingcsharpjavaandroid
5 Posts 2 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have been trying FOREVER to get a simple Xamarin Forms app to call a Web API. I am running this Microsoft sample [Xamarin.Forms - TodoREST - Code Samples | Microsoft Docs](https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/webservices-todorest/) The only change I made was the port number that the API uses. This line is in a constants file. The emulator uses the internal ip of 10.0.2.2:

    public static string RestUrl = DeviceInfo.Platform ==
    DevicePlatform.Android ?
    "http://10.0.2.2:44300/api/todoitems/{0}" :
    "http://localhost:44300/api/todoitems/{0}";

    Here's the relevant code. The URI being passed is correct from the line above:

    public async Task> RefreshDataAsync()
    {
    Items = new List();

    Uri uri = new Uri(string.Format(Constants.RestUrl, string.Empty));
    try
    {
        HttpResponseMessage response = await client.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            string content = await response.Content.ReadAsStringAsync();
            Items = JsonConvert.DeserializeObject>(content);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(@"\\tERROR {0}", ex.Message);
    }
    
    return Items;
    

    }

    It throws this exception:

    {Java.Net.SocketException: Connection reset
    at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in :0
    at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in :0
    at Java.Net.HttpURLConnection.get_ResponseCode () [0x0000a] in :0
    at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass46_0.b__2 () [0x00000] in :0
    at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:534
    at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319
    --- End of stack trace from previous location where exception was thrown ---

    at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I have been trying FOREVER to get a simple Xamarin Forms app to call a Web API. I am running this Microsoft sample [Xamarin.Forms - TodoREST - Code Samples | Microsoft Docs](https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/webservices-todorest/) The only change I made was the port number that the API uses. This line is in a constants file. The emulator uses the internal ip of 10.0.2.2:

      public static string RestUrl = DeviceInfo.Platform ==
      DevicePlatform.Android ?
      "http://10.0.2.2:44300/api/todoitems/{0}" :
      "http://localhost:44300/api/todoitems/{0}";

      Here's the relevant code. The URI being passed is correct from the line above:

      public async Task> RefreshDataAsync()
      {
      Items = new List();

      Uri uri = new Uri(string.Format(Constants.RestUrl, string.Empty));
      try
      {
          HttpResponseMessage response = await client.GetAsync(uri);
          if (response.IsSuccessStatusCode)
          {
              string content = await response.Content.ReadAsStringAsync();
              Items = JsonConvert.DeserializeObject>(content);
          }
      }
      catch (Exception ex)
      {
          Debug.WriteLine(@"\\tERROR {0}", ex.Message);
      }
      
      return Items;
      

      }

      It throws this exception:

      {Java.Net.SocketException: Connection reset
      at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in :0
      at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in :0
      at Java.Net.HttpURLConnection.get_ResponseCode () [0x0000a] in :0
      at Xamarin.Android.Net.AndroidClientHandler+<>c__DisplayClass46_0.b__2 () [0x00000] in :0
      at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:534
      at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319
      --- End of stack trace from previous location where exception was thrown ---

      at Xamarin.Android.Net.AndroidClientHandler.DoProcessRequest (System.Net.Http.HttpRequestMessage request, Java.Net.URL javaUrl, Java

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Have you allowed traffic to that port in the Windows Firewall?


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Have you allowed traffic to that port in the Windows Firewall?


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Do I need to do that on my Dev PC using the Xamarin Forms emulator?

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        Richard DeemingR 1 Reply Last reply
        0
        • K Kevin Marois

          Do I need to do that on my Dev PC using the Xamarin Forms emulator?

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          As far as I can see, you do. Set up Android Emulator networking  |  Android Developers[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          K 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            As far as I can see, you do. Set up Android Emulator networking  |  Android Developers[^]


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #5

            I opened the port, and get the same error

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups