localhost

The post shows how to access localhost:

on Android Device and on Android Emulator

ACCESSING LOCALHOST ON ANDROID DEVICE

To access it on your Android Device, you need to find the computer’s IP address.

  • The most general way of finding this info, which works on both Mac and Linux, is  opening a Terminal and typing “ifconfig” Now Windows users you can  do Run->cmd and type “ipconfig” in the command prompt.
  • Look for either “en0″ or “en1″, and under that entry look for the “inet” listing. It will be something along the lines of “192.168.1.100”.
  • When you find that address, that’s what you’ll want to put in your browser’s address bar or the URL in the code you want to call your MAC’s/Windows’ localhost.
  • On a Mac specifically, 

    Go to System preferences/sharing OR just search on right top – remote management.

    **Make sure remote management is checked.

    Note : MAKE SURE THAT YOUR ANDROID DEVICE AND YOUR MAC/WINDOWS ARE ON THE SAME NETWORK OTHERWISE IT WILL NOT WORK. [EG : SAME WIFI NETWORK]. 

For Example in the Android code you can use like below and call a webservice that is residing in your local server, i.e your localhost.

 

new HttpPost("http://192.168.0.101/your_localhost_folder/your_localhost_file.php"); 

ACCESSING LOCALHOST ON ANDROID EMULATOR

You can have the same thing as described above for Android Emulator also as long as they are in same network.

If you are trying to access a file (PHP or java) residing in your server in your own machine, then
you have to use a specific IP to access it. If it is public, then use the other System’s IP

  • If it is localhost, In Android you use it as –>  10.0.2.2

The localhost refers to the device on which the code is running, in this case the emulator.

  • If you are trying to access from iOS Simulator use can use –> http://localhost:8000/your_path

The iOS Simulator uses the host machine network so you should be able to just use localhost or your machines IP address, whichever IP your web service is listening on.

android-localhost