If you are using WAMP server to test your PHP files locally, Android localhost connection and testing PHP in your mobile phone is easy.
If you are working on PHP MySQL with your android app, it’s essential that you need to test your app for correct data before moving your database, PHP files and Android app to production. One way you can test for the same is, going live on the internet by hosting your files with hosting provider and it’s a bad idea to test your files live on the internet.
Rather than hosting your database and PHP files on the internet, you can test your app for PHP MySQL using localhost WAMP server. To do Android localhost connection, you need to install and setup WAMP server on your windows computer. If you don’t have WAMP server installed on your computer then you can refer tutorial Setup and Install WAMP Server.
People who already installed WAMP may try connecting android phone by entering
http://localhost/ in your android code but that does not work for you because your phone does not understand what localhost is, and localhost is only an internal reference to your computer. Follow below tutorial to connect Android with localhost using IP address.
Successfully Running WAMP server on your computer indicates a green icon on system tray, click on that, with a menu pop up click on put online item. Wait for few second to process its task. below is the screenshot of wamp menu.
Now, you need to find your IPV4 address of your computer, run command prompt in your computer(go to Windows start button and start typig cmd) and type
ipconfig and enter to see your IPV4 address of your computer. Below is the screenshot of command prompt.
Open your web browser and type your computer IPv4 address and go, for example
http://192.168.1.7/. by doing so you likely to see your localhost page. Now, you can access any page in localhost through your computer IP address.
If you don’t know how to connect android to PHP MySQL, you can refer some of tutorial below.
- Android to fetch PHP file without using any library
- Fetch JSON data in Android and display with RecyclerView
- Android PHP Mysql login tutorial using HttpURLConnection
- Android PHP/MYSQL Tutorial – TutorialsPoint
Android connect localhost in Emulator
To test your app for Android localhost connection with PHP MySQL in your android studio emulator, just enter address as
http://IP Address/ followed by path of your PHP file in your code.
Note: Before putting your address into code, test address in your browser for whether path is valid or not.
Android connect localhost in your Android Mobile Phone
Enter address as http://IP Address/ followed by path of your PHP file in your code.
To test your app for Android localhost connection with PHP MySQL in Android Mobile Phone, your computer and mobile phone should be connected to same Wi-Fi router.
Note: Before putting your address into code, test address in your mobile browser for whether address is accessible or not.
What if i don’t have Internet or Wi-Fi router?
If you don’t have internet or Wi-Fi router connected, you can use below method that may work for you.
- method1: Create new wireless network in your windows computer and connect your android phone through Wi-Fi.
- method2: Install any Virtual Router and connect your phone through Wi-Fi.