It’s a minor problem but it can cause headaches, connecting Android emulator to localhost is one of the important things we should care about. By default, the Android emulator will automatically recognize 10.0.2.2 as localhost so it is easy to develop API on a local machine.
But what if it doesn’t work that way? Hmm.. a lot of people struggling to find a way to connect to localhost
Using Default IP
The native solution is much better than using a third-party application, and here is the easiest way to use the default IP. The key is to start localhost using 127.0.0.1 on our localhost server, then 10.0.2.2 on the emulator. And don’t forget to use the same port both localhost and emulator.
- Start localhost by using
10.0.2.2on your API URL inside your project
- Use the same port for both URL
Let’s the image below tell us more 😀 we can use the native PHP Server command to start localhost
php -S 127.0.0.1 -t public. If you use Lumen framework, don’t forget to add “public” as an entry directory.
If you want the other ways, there are several third-party software that can be used such as ngrok. By ngrok, you can connect the emulator as well as real devices to localhost.
Ngrok provides a real-time web UI where you can introspect all HTTP traffic running over your tunnels. Replay any request against your tunnel with one click.
You can run ngrok by typing
ngrok http 80 in console or terminal. For Mac user, we used dot back-slash before ngrok command such as
./ngrok http 8800 or using optional parameter region,
./ngrok http 8800 -region=us for US or
./ngrok HTTP 8800 -region=ap for the Asia Pacific. Use your own port to connect.
I think this software is very useful if we want to show progress to the client. By providing Ngrok Url, we can test mobile app to real devices if we don’t have any server deployed yet.
Due to heavy traffic,
ngrok can sometimes be very slow. You should purchase a premium service for better network handling and customization.
Another option is to use a special proxy to match your local port or use Squidman app proxy.
Squidman can be powerful for testing the responsiveness of websites as well as local APIs for development on real devices. But this couldn’t be easier due to the lack of documentation. Of course, it will be an interesting challenge for us. Connecting Android emulator to localhost will also be easy and tricky.
Maybe next time, I will write another story about using this
Squidman application, because I have also used this software to test the responsiveness of the website directly to the mobile device browser, it gives me a nice feeling and exciting experience.
What if the emulator can’t connect to internet?
Another issue about connecting Android emulator to localhost is that it can’t connect to the internet. But it’s about your DNS or internet provider issue. This has nothing to do with the things we discussed earlier.
The emulator sometimes needs to connect to the internet if we are using a network image for example, so the internet is an issue. We have to switch to another network or try to use Google public DNS, but I prefer the first solution by bringing my portable modem 😀 and switching the laptop network to this modem.