How to Run Android Apps Against a localhost API (Tutorial)

preview_player
Показать описание
In this video we're showing you how to run an Android app against a localhost API. We demonstrate a real device and an emulator approach.

Tip: turn on subtitles to deal with my accent. A full transcript is available below.

More information in the tutorial:

Watch 20+ Retrofit videos in our playlist here:
----------------------------------------

Checkout 320+ technical in-depth tutorials:

Subscribe for two new videos every week:

----------------------------------------

Follow us on social media to get updates on new content:

----------------------------------------

Full transcript:

In this video I'm going to show you how to run an Android app against a localhost API. In your Android project you probably have some kind of base URL, which will resolve to your URL. Obviously, if you're running an API on your local machine, this would not work. Depending on your setup, you most likely have some kind of localhost and port address. In our case, it's localhost:3000, which we'll run our server with all the API endpoints.

Obviously, you cannot use localhost in your Android app since that will resolve to nothing on the phone. The trick is to use the IP address of your computer and then the port. So in my case this is this, in your case that's most likely something else. Use the network utility to figure that out. Copy the entire address over and make that your API base URL. If your phone and your computer are on the same network, this will run everything in the app against the localhost API.

Unfortunately, this doesn't work for emulators. An emulator cannot resolve the IP address or the localhost to your machine. Instead, you've use a special kind of IP address, which is 10.0.2.2. If you add the port, it'll demonstrate exactly what's on your localhost of the hosting machine. If you want to test your app on an emulator against the localhost API you've to change the IP address one more time and build the apk, run it and you can test the local API on your emulator.

If you've any questions, let us know in the comments. I've linked a blog post with all the information and the IP address in the description below. If you like this video, feel free to subscribe to us on YouTube. Happy coding.
Рекомендации по теме
Комментарии
Автор

Adding cordova-plugin-cleartext with your solution worked for me for my cordova builds. Thank you

CameronCobb
Автор

People like you are the real savior...

ShuvamBosana
Автор

I keep getting a bad request invalid host name

Ricardo-Gonzalez
Автор

Bless your soul. I've been struggling to get this emulator working for weeks

ifeanyiorazuluka
Автор

Thanks for the video, was struggling with this.

nicohaverdil
Автор

thank's, you got a subscriber :)

AiEnthusiast
Автор

are there a year 2020 solution for connecting to local API which is running under HTTPS instead of HTTP only? Thanks

JaraFPV
Автор

Trying to run api on actual android device using IP address to the server. It doesn't work please help me if you have any idea regarding this. It works fine when I run it on Android device browser.

mihirraval
Автор

Im confused as to how it acces my local web api, i mad one in c# and how is it supposed to know how to access it? i see that when i run the api it says loachost:60060 assuming the numbers are the port it still doesnt work? or s ther a way to verify that it conncected succefully?

Ricardo-Gonzalez
Автор

Would this work if the android studio app and the api rest are on separate machines but in the same network?

manilkesouri
Автор

i migrated my android apps from xamp server to wamp server on my pc, the app i build works fine with xamp but the same app isn't responding with wamp server .any idea why this is so?

drstephenzulujr
Автор

I don't know why but "10.0.2.2" doesn't work in many case with android simulator

bigo
Автор

hello sir, Could you tell me about run Android real device connect with laravel api.

vichittech
Автор

Did I miss the video on how to setup a localhost API?

anewbeginningquinn
Автор

How can i access the same localhost api on outer network pc

mr.curious
Автор

I get an error Failed to connect, any idea ?

yamenemad
Автор

i am unable to get Value when i connect my android phone as a emulator.

gadgethubpaymentportal
Автор

Hi, on my computer there are a lot of project all using localhost, I have setup different domain names for them in the vhosts.conf and hosts files on windows, how would I access one specific project this way?

nanchitacr
Автор

Hi,
I am facing issue while connecting to localhost I am using Visual Studio 2017 and Nexus_5_26 emulator from Android Studio. After starting localhost from VS in Chrome. Keeping it with 10.0.2.2:45454 in Emulator's Chrome browser, It is showing invalid request.
Thank you

arvindchourasiya
Автор

can you please explain why we need to use 10.0.2.2 instead 192.168.0.1

bagwanakib