Xamarin Forms Async Call Never Returns
-
I'm trying to learn to call an Asp.Net Web API from a Xamarin Forms app. I'm testing my app using my phone plugged into my Dev PC running from VS 2019. Here's my code so far:
private void Button_Clicked(object sender, EventArgs e)
{
CustomerEntity customer = Test(1).Result;
}
public async Task Test(int id)
{
var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));var response = await client.GetAsync(url); // Nothing happens after this!! CustomerEntity cust = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return cust; }
}
I can paste the URI into a browser and get results. When I click the button on my app that calls the above, I get no response after the GetAsync. Anyone have any idea what's going on or how to debug this? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I'm trying to learn to call an Asp.Net Web API from a Xamarin Forms app. I'm testing my app using my phone plugged into my Dev PC running from VS 2019. Here's my code so far:
private void Button_Clicked(object sender, EventArgs e)
{
CustomerEntity customer = Test(1).Result;
}
public async Task Test(int id)
{
var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));var response = await client.GetAsync(url); // Nothing happens after this!! CustomerEntity cust = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); return cust; }
}
I can paste the URI into a browser and get results. When I click the button on my app that calls the above, I get no response after the GetAsync. Anyone have any idea what's going on or how to debug this? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Quote:
var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
Unless your Android (or the mobile device) has a local server running on the machine that can respond to a localhost:44302 request, you will not receive a response and request will likely timeout and fail. There are several ways to connect to a localhost based website: 1. You have a physical Android device that you are using for testing 1. You are using a virtualized device for testing If you have a physical device, then try to send the request to the host (your development machine)'s IP address with the port number; for example, change
localhost:44302
to192.168.1.17:44302
(assuming your machine's address on the same network is 192.168.1.17) If you are using the virtualized then this gets complicated and depends on several factors; what network setting is being used, which OS you are using, does the virtualization software support host-connections, etc. etc. Oh, and the best of these is to use an online proxy that can tunnel your Android device with the local machine; something like NgRok can help here. [ngrok - secure introspectable tunnels to localhost](https://ngrok.com/)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Quote:
var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
Unless your Android (or the mobile device) has a local server running on the machine that can respond to a localhost:44302 request, you will not receive a response and request will likely timeout and fail. There are several ways to connect to a localhost based website: 1. You have a physical Android device that you are using for testing 1. You are using a virtualized device for testing If you have a physical device, then try to send the request to the host (your development machine)'s IP address with the port number; for example, change
localhost:44302
to192.168.1.17:44302
(assuming your machine's address on the same network is 192.168.1.17) If you are using the virtualized then this gets complicated and depends on several factors; what network setting is being used, which OS you are using, does the virtualization software support host-connections, etc. etc. Oh, and the best of these is to use an online proxy that can tunnel your Android device with the local machine; something like NgRok can help here. [ngrok - secure introspectable tunnels to localhost](https://ngrok.com/)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
I'm using my phone plugged into my PC. When I paste this into the browser I get data back:
"https://localhost:44302/api/Customer/Get?Id=1"
When I paste this into the browser
"https://192.168.1.149:44302/api/Customer/Get?Id=1"
I get
Bad Request - Invalid Hostname
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I'm using my phone plugged into my PC. When I paste this into the browser I get data back:
"https://localhost:44302/api/Customer/Get?Id=1"
When I paste this into the browser
"https://192.168.1.149:44302/api/Customer/Get?Id=1"
I get
Bad Request - Invalid Hostname
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Quote:
I'm using my phone plugged into my PC.
I don't think that unless you also share the network via USB interface, you can access the PC's `localhost`.
Quote:
When I paste this into the browser I get data back: "https://localhost:44302/api/Customer/Get?Id=1"
Because your "PC browser" has a process that is internally listening on the `localhost` (`127.0.0.1`) @ `44302`. So it works.
Quote:
When I paste this into the browser "https://192.168.1.149:44302/api/Customer/Get?Id=1"
This would "only" work if you have a machine or a process that is listening on
192.168.1.149
@44302
. This is more likely a networking issue rather than an Android/PC problem. And, when you are on the same machine, you should always use `127.0.0.1` (aka `localhost`) instead of the machine's own private IP address. Oh, one more tip, try onlyhttp://
and not thehttps://
, or you will end up running in the SSL errors and you might assume the machine is down.The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Quote:
I'm using my phone plugged into my PC.
I don't think that unless you also share the network via USB interface, you can access the PC's `localhost`.
Quote:
When I paste this into the browser I get data back: "https://localhost:44302/api/Customer/Get?Id=1"
Because your "PC browser" has a process that is internally listening on the `localhost` (`127.0.0.1`) @ `44302`. So it works.
Quote:
When I paste this into the browser "https://192.168.1.149:44302/api/Customer/Get?Id=1"
This would "only" work if you have a machine or a process that is listening on
192.168.1.149
@44302
. This is more likely a networking issue rather than an Android/PC problem. And, when you are on the same machine, you should always use `127.0.0.1` (aka `localhost`) instead of the machine's own private IP address. Oh, one more tip, try onlyhttp://
and not thehttps://
, or you will end up running in the SSL errors and you might assume the machine is down.The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
This is all confusing to me. All I want to do is write a simple test Xamarin Forms app that connects to a Web API. I'm testing on my phone which is connected via USB to my PC. WHat's the right way to do this? Can you point me to any resources for consuming a web API from Xamarin Forms?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
This is all confusing to me. All I want to do is write a simple test Xamarin Forms app that connects to a Web API. I'm testing on my phone which is connected via USB to my PC. WHat's the right way to do this? Can you point me to any resources for consuming a web API from Xamarin Forms?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
I think if you use the emulator in vs2019 you may be able to access localhost (a guess). The issue is that the phone is a separate device without the infrastructure to access you PCs IIS server.
Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP
-
Quote:
var url = "https:" + "//localhost:44302/api/Customer/Get?Id=1";
Unless your Android (or the mobile device) has a local server running on the machine that can respond to a localhost:44302 request, you will not receive a response and request will likely timeout and fail. There are several ways to connect to a localhost based website: 1. You have a physical Android device that you are using for testing 1. You are using a virtualized device for testing If you have a physical device, then try to send the request to the host (your development machine)'s IP address with the port number; for example, change
localhost:44302
to192.168.1.17:44302
(assuming your machine's address on the same network is 192.168.1.17) If you are using the virtualized then this gets complicated and depends on several factors; what network setting is being used, which OS you are using, does the virtualization software support host-connections, etc. etc. Oh, and the best of these is to use an online proxy that can tunnel your Android device with the local machine; something like NgRok can help here. [ngrok - secure introspectable tunnels to localhost](https://ngrok.com/)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
I also face the same issue. Looking forward to get the right one.