Connect Android App to Localhost Web Server Over Wi-Fi

Connect Android App to Localhost Over Wi-Fi

Access or connect android app to localhost over Wi-Fi and it will give you a lot flexibility to develop, run and test the app. My scenario was that I had created an android app which had need to access my local web server (XAMPP) for faster testing and the application was running on an emulator as well as device.

You might be either using PHP + MySQL or C# + MSSQL on Apache / IIS  Server respectively. And you need to code and test your app before moving them to production. You can access or connect android app to local web server over Wi-Fi, without need of internet connection and eliminating the bad idea to communicate host or your live server.

Know what is Android?

I believe that you have already setup your localhost and files to serve the request being receive from device. Ensure that your local server is accessible (type http://localhost/ in your PC’s browser and get response). The article is intended to answer the following similar questions:

  • Test local website on mobile device
  • Run android app against a localhost API
  • How to access localhost from mobile browser
  • How to access localhost from android over Wi-Fi
  • Connecting android app to your WAMP / XAMPP server
  • Connecting to localhost web server from android application

Connect Android App to Localhost Over Wi-Fi

Simply typing localhost or http://127.0.0.1 in your app code won’t work. You need to obtain IPv4 address of your PC / server. If you’re using Windows PC, you can follow these steps to find IPv4 address:

  • On Start menu, type cmd to open Command Prompt.
  • Run ipconfig command there.
  • You will get IPv4 address as highlighted in image below.

Connect Android App to Localhost Over Wi-Fi

As you can see my IPv4 address is 192.168.1.110. However some users worry, do each time they need to check IP? No, absolutely not. Let me tell you three options.

  • Generally it’s needed to check only when your router gets reset.
  • In my case, I had set IP address pool range in my VDSL modem and I’m getting the same address for my local server since years regardless of PC / router restart. I have mentioned how I did it to connect android app to localhost running in my computer.
  • Third is routers / modems give you option to reserve IPv4 address for specific device and you can assign fixed IP to your local server, fix once & always use it to access localhost from mobile browser or your app.

Setup IP Pool Range in VDSL for Lasting Server Address

This step is optional and based on my personal experience. You generally don’t need it; try only if your IPv address varies too much. Please note that these are general instructions only and might vary from router to router. 

  • In your PC / laptop access http://192.168.1.1 and give adequate credentials.
  • You will have your router configuration page.
  • Find the DHTP Server Settings page there.

IP address pool range android app localhost

On this page, set IP Pool Range and save changes. An IP Pool Range is a set of IP address range used by your router / modem to assign IPs to connected wired or wireless devices. And no connected device like mobile, PC or laptop will have IP address out of this range then. After doing this, don’t forget to restart the local server to take effect.

This page also shows you list of connected devices along with their Mac Address. We will use it if this pool range setting doesn’t work for you.

Assign Fixed IP to Your Local Server and Access it in Mobile

Server static IP from Router

Lastly, you can assign a permanent IP address to your local web server if above is not sufficient. Find DHTP Reserved or DHCP Static IP Configuration page in router configuration pages. You need to have Mac Address of localhost PC or there is plenty of information available on internet to find Mac Address of a device.

Give one desired IP address from range 192.168.1.2 to 192.168.1.255 (192.168.1.1 is normally address of router itself). Specify Mac Address (without colons if instructed) of local web server machine and save it. Restart your machine; confirm address using ipconfig command and you’re done.

Access Your PC / Localhost from Android Code

Till here, you have gotten stable IP address for your server. Now it’s time to use the address in android code. Before that try to connect android browser with server using url http://192.168.1.110 (or your localhost url along with port no if using any rather than default: 80). You can see response from server.

Now for the app to test, ensure your app has internet access permission. Open AndroidManifest.xml file and add following line if not exists:

Next in your class file use IP address of local web server explicitly, not the term localhost. For a PHP + MySql as server side language used, it would be IP address followed by path, something like:

No matter now you’re using USB to debug or Android Emulator to test your app, you can connect android app to localhost over Wi-Fi to develop, run and debug without using live server. The same is true to access localhost from mobile browser.

You Might Interested In