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

Показать описание
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.
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.
Комментарии