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

Published: 09 February 2016
on channel: Future Studio
88,115
312

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:
►https://futurestud.io/tutorials/how-t...

Watch 20+ Retrofit videos in our playlist here:
►   • Android Retrofit Tutorial Series  
----------------------------------------

Future Studio is helping 5,000+ users daily to solve Android and Node.js problems with 320+ written tutorials and videos. We’re on a mission to provide new in-depth content every week.

Checkout 320+ technical in-depth tutorials:
►https://futurestud.io

Subscribe for two new videos every week:
►https://www.youtube.com/c/FutureStudi...

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

Follow us on social media to get updates on new content:
►  / futurestud_io  
►  / futurestudiouniversity  
►https://plus.google.com/+FutureStudio...

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

Full transcript:

Hey, what is up everyone, Norman from futurestud.io University here.

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.


On this page of the site you can watch the video online How to Run Android Apps Against a localhost API (Tutorial) with a duration of hours minute second in good quality, which was uploaded by the user Future Studio 09 February 2016, share the link with friends and acquaintances, this video has already been watched 88,115 times on youtube and it was liked by 312 viewers. Enjoy your viewing!