This was a bit tricky but it makes sense.
When writing your code, keep in mind that "localhost" refers to the device which the code is running on; the mobile phone or tablet which is using Android.
If you want to refer to the computer which is running the Android simulator, use the IP address "10.0.2.2" instead.
This will direct the simulator to the simulator host.
[ Source ]