Xamarin Forms API - Can't Connect
-
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
-
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
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
-
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
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.
-
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.
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
-
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
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.