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:
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:
Subscribe for two new videos every week:
Follow us on social media to get updates on new content:
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.
Xem thêm Bài Viết:
- Đọc ngay hướng dẫn cài twrp Recovery hiệu quả trên thiết bị Android
- Bỏ túi cách thêm IDM vào Chrome tiện lợi không phải ai cũng biết
- Bỏ túi thông tin hướng dẫn sử dụng adb fastboot zenfone 5 flash Asus
- Hướng dẫn cài đặt và sử dụng Adobe Audition 1.5 chi tiết nhất
- Khắc phục lỗi ‘adb’ is not recognized as an internal or external command