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.
For example: http://192.168.1.7/test/example.php
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.
For example: http://192.168.1.7/test/example.php
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.
May 9, 2019 at 12:48 pm
Hi, I am new to android studio, i have gone through your code,i’m getting oops! something went wrong, connection problem. my login.php is showing connected successfully, but my app is showing this and I have changed url localhost into ip address. can u help me? please.
May 9, 2019 at 2:39 pm
Please test you IP address in mobile browser and check for proper data.
June 17, 2018 at 3:32 pm
if I run my android application on the real phone and I want to insert a record into the database on localhost than what is the URL(IP address)??
June 18, 2018 at 2:38 pm
The URL is your computer IP address. Make sure that both PC and device connected to same wifi network.
May 5, 2017 at 7:42 am
Man, I don’t know. It is a very clear explanation of things.
April 12, 2017 at 7:29 am
Please look at your logcat for errors.
March 25, 2017 at 12:02 pm
it doesn’t work address ip the problem is unsuccessfully
if there is another idea please tel me thanks
April 12, 2017 at 7:46 am
Could you please elaborate it in detail.
February 14, 2017 at 3:47 am
When I try to access my php file through mobile browser it gives me error forbidden, I am using ip address as suggested here, please help
February 8, 2017 at 2:04 pm
I followed your code and used my PC
ipv4
address but i am getting connection failed message.My wamp server is running perfect.I checked address with mobile browser and it is showing you don’t have permission to access server. What might caused the issue?
March 22, 2017 at 12:14 pm
Make sure that you are connected to same wi-fi router, to your mobile as well to your PC. Even though, if it is not working then your router may incorrectly setup or firewall is blocking your request.
March 28, 2017 at 6:22 am
Can’t we use different connection for mobile and pc
April 12, 2017 at 7:45 am
I’m not clear with your question.
February 4, 2017 at 7:27 am
i test my ip address to my mobile device browser before i put into my codes, the problem is , the ip address is not accessible… it’s just load too long..how to i fix that?? plss help..this is for my thesis
March 22, 2017 at 12:07 pm
The problem may be caused by your wi-fi router or firewall.
October 24, 2016 at 7:26 am
Very Nice and very useful tutorial. I was stuck into the same issue from last night, but using this IP solution it solved my problem.
Thank you Gururaj
October 11, 2016 at 11:51 am
hello everyone i make my android application and i convert it to .APK after this step my application can’t connect to wamp server . what can i do to work it like it was in an emulator thnx
October 11, 2016 at 3:53 pm
Make sure the address you entered is available/test with mobile browser/chrome.
June 16, 2016 at 6:03 am
Its not working when I use SDK version 5.0(lollipop) and above. But when I use API 20, 4.4(kitkat) and below it works.
June 16, 2016 at 6:38 am
May i know, what portion is not working or any error you are getting? I’m using SDK version 6.0(Marshmallow) to test my app and it’s working fine with me. Usually, it does not depend on the SDK version you are running. May be your IP address changed recheck it and also check WAMP for put online option.
June 14, 2016 at 5:49 pm
Can You please provide Android and php code for forget password option sent to email to reset password. Thanks in advance
June 14, 2016 at 6:35 pm
Soon i will going to write tutorial on topic “sending email through android” and will notify you once it is done. I’m sure at that time i provide code what you requested.
June 1, 2016 at 12:40 pm
can you make a tutorial to insert and retrive rows into mysql database from an android application using php and wamp server?
June 1, 2016 at 12:48 pm
sure…there is already tutorial on how to retrieve data(Login Tutorial) and for insert i’ll come up with tutorial soon.
June 2, 2016 at 5:46 am
Thank you so much, the login tutorial worked like charm .I have seen many websites but all of them were outdated.I’ll be glad if you can make a tutorial to insert and display in an android device as soon as possible.
Thanks in advance
May 29, 2016 at 6:14 am
Each time I have to change IP??
May 29, 2016 at 6:29 am
No. You need to check for new IP only when your router get reset.