I have created an API in C#, Visual studio and it is running on my localhost with address http://localhost:xxxxx/api/allproduct.
I have also created an Android app in Android Studio and tried to access the above API. I am debugging my app using data cable (Not by emulator). I am not able to access the API.
I used chrome://inspect/#devices and with the help of port forwarding I am able to access api in my mobile's browser. But when I am trying to connect this API through code (app code in Android studio), it is telling "could not connect to http://localhost:xxxxx/api/allproduct".
Any one Please suggest any alternate.
Thank you everyone for suggestions. I used you ideas and added some more. I ran my api with IP and used chrome://inspect/#devices for port forwarding and after that I opened this api in my mobile browser(USB cable connected). After that I started to debug my android application which was using the api. it ran successfully.
Point I noted that when api was not running in my mobiles browser, my android application was crashing and when api was running, app was not crashing.
I was accessing api in my mobiles browser by localhost:xxxx/api/product
Remember, localhost is *"this machine."
In other words, "to your Android, it's your Android!"
Your phone is talking to itself, not to the machine that's running your API. You must provide (or, enable the phone to locate ...) the IP-address of that machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With