|
|
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<CustomerEntity> 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);
CustomerEntity cust = JsonConvert.DeserializeObject<CustomerEntity>(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:
- You have a physical Android device that you are using for testing
- 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 to 192.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
The sh*t 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.
|
|
|
|
|
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 only http:// and not the https:// , or you will end up running in the SSL errors and you might assume the machine is down.
The sh*t 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.
|
|
|
|
|
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
|
|
|
|
|
I also face the same issue. Looking forward to get the right one.
|
|
|
|
|
How do I create an operating system using C programming language, and install it on my smartphone
|
|
|
|
|
You would need to write device drivers, boot sequence, kernel, support libraries etc. It will probably take a while.
|
|
|
|
|
It's up to you. Where are you stuck?
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I'm going to be bulding a Xamarin Forms app for a client in the construction industry. I can publish directly to my tablet via Visual Studio. But what options are there for gettting the app and updates to my client's tablets?
This isn't going to be a commercially available app that anyone can download from the app store. It's only for use on my client's tablets.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Hi,
Iam using VS2015, Windows-10 Home. When I try to install Visual Studio Android Emulator in Windows 10 Home, It asked me to install Hyper-V. Then I installed. But now Iam getting the error message
Visual Studio Android Emulator can not install in Windows 10 Home
Thanks
|
|
|
|
|
The message is clear, the Android emulator cannot be run on that version of Windows.
|
|
|
|
|
|
|
Hi,
I am using VS2015, Windows-10 Home. When I try to install, Visual Studio Android Emulator, it asked me to install Hyper-V, and then I installed. But now Iam receiving the message like Visual Studio Android Emulator can not installed in Windows 10 Home. Is there any solution.
Thanks
|
|
|
|
|
Hi,
I am totally new to mobile app development, i am looking to develop a mobile app (hybrid) where a user can flip through a book (pdf) pages and place book marks where required.
I need some guidance as to which tech stack / framework to use. Any pointers will be of great help. If there is any similar app or sample project that will be awesome.
Regards,
SA
|
|
|
|
|
Member 14721287 wrote: I am totally new to mobile app development In that case you first need to decide which language to use. If you already know Java then go for Android Studio, if C# then you may like to try Xamarin Forms. If you are not experienced in either of these languages then you have a long way to go before you can even start to think about how to build this application. There are lots of articles at Mobile Development[^] which can help.
|
|
|
|
|
Thanks Richard,
I am quite comfortable with C#, i was wondering if i can do something with ionic or any other mobile dev framework.
Regards,
SA
|
|
|
|
|
|
Sure ill check xamarin and also wait if someone else might have some other suggestions.
Thanks once again.
Regards,
SA
|
|
|
|
|
Just have to agree on Xamarin, imo the best option out there
|
|
|
|
|
I'm a long time WPF developer. I am working on a WPF business app for a client and I'm going to be developing an Android tablet app to go with it. This is my first Xamarin Forms project, so bear with me...
I followed the tutorials here Xamarin documentation - Xamarin | Microsoft Docs under "Get started with Xamarin.Forms", "Build Your First App". I went through all four projects. Everything works fine.
So I then decided to convert the project to MVVM. One thing that I found through Googling is that if you want a View Model to open a new page, your View Model has to know about the INavigation property on the Page See this. What concerns me is that INavigation has to be passed from the UI into the CTOR of the VM. IMHO, This creates a few problems:
1) The VM must be set in the CTOR of the view. That prevents you from doing this.
2) A UI property now has to be passed to the VM.
3) Doing it this way means that you cannot share VMs between WPF and Xamrin Forms projects as WPF doesn't have INavigation.
One this last point... I got the impression from reading up on Xamarin Forms that it's all about sharing code. It seems to me that I could have VMs in a shared project, and use the them in both WPF and Xamarin Forms apps. For example, a LoginViewModel is a good candidate.
Again, I'm very new to Xamarin Forms. I would like to share VMs if I can. I'm open to hearing how this should work.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 28-Jan-20 11:18am.
|
|
|
|
|
hi Kevin
Why do you want to mix Xamarin form and WPF if i may ask ?
You can create a Xamarin Standard Project and create the content pages there and other types of pages or views you want to add , after that do your UI there and if there is a limitation on the Xamarin Forms you can use Custom Renders and this UI changes you have made in the standard form will be outputted to different platforms which is ios and Android.
i know you have knowledge of WPF , you can use it on Xamarin Form. but leave WPF out of Mobile dev for now.
Vuyiswa Maseko,
Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code.
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vimalsoft.com
vuyiswa[at]vimalsoft.com
|
|
|
|