I have an android device (Samsung galaxy tab) connected to my PC via USB .
I want to use WebServices and run a web page which is located on my local xampp server of my PC on my android device .
I can achieve it if I have both , my PC and android device , connected on the same wireless network .
However is it possible to access the localhost page without any wireless connection , using only USB connection .
(PS : This same scenario works when I run the app on an emulator and use the ip 10.0.2.2 to access the localhost on my PC)
Google posted a solution for this kind of problem here.
The steps:
Connect your Android device and your development machine with USB debugging enabled
Open Chrome in your development machine, open new tab, right click in the new browser tab, click inspect
Click the three dots icon on right top side , -> More Tools, Remote Devices.
Look at bottom of the screen, make sure your device name is appeared on the list with Green colored dot.
Look below at the settings part, check the Port forwarding mark
Add rule. Example, if your python web server is running on your machine localhost:5000 and you want to access it from your device port 3333, you type 3333 on the left part, and type localhost:5000, and click add rule.
Voila, now you can access your web server from your device. Try open new browser tab, and visit http://localhost:3333 from your device
I finally solved this problem. I used Samsung Galaxy S with Froyo. The "port" below is the same port what you use for the emulator (10.0.2.2:port).
What I did:
first connect your real device with the USB cable (make sure you can upload the app on your device)
get the IP address from the device you connect, which starts with 192.168.x.x:port
open the "Network and Sharing Center"
click on the "Local Area Connection" from the device and choose "Details"
copy the "IPv4 address" to your app and replace it like: http://192.168.x.x:port/test.php
upload your app (again) to your real device
go to properties and turn "USB tethering" on
run your application on the device
It should now work.
I've read numerous forums and tried play apps but not found a solution until now.
My scenario I believe is similar to yours, but I will clarify to help others. I have a locally hosted website and web services to be used by my android application. I need to have this working on the road for demonstration with only my laptop and no network connection.
Note: Using my iPhone as a wifi hotspot and connecting both my pc and my android device worked, but the iPhone 4S connection is slow and dropped out regularly.
My solution is as follows:
Unplug network cables on PC and turn off wifi.
Turn off wifi on android device
Connect android to pc via USB
Turn on "USB Tethering" in the android menu. (Under networks->more...->Tethering and portable hotspot")
Get the IP of your computer that has been assigned by the USB tether cable. (open command prompt and type "ipconfig" then look for the IP that the USB network adapter has assigned)
Open a browser on the PC using the IP address found instead of localhost to test. i.e. http://192.168.1.1/myWebSite
Open a browser on the android and test it works
I did this on a windows computer and it worked perfectly!
Turn on USB Tethering in your mobile.
Type ipconfig in the command prompt in your computer and find the ipv4 for "ethernet adapter local area connection x" (mostly the first one)
Now go to your mobile browser, type that ipv4 with the port number of your web application. eg:- 192.168.40.142:1342
It worked with those simple steps!
Here is a piece of my Android app's code:
This app is able to communicate with a HTTP get-post model between a servlet running on a server and an Android device plugged in USB-Debuggable mode (because the app was in developing progress).
I also can run the app over Wi-Fi when the server, Tomcat Apache 7, running on (when the app development was finished).
To get the IP address of yours
Go to Command Prompt
Type ipconfig
Hit enter
In the list, IPv4 Address is your IP.
Make sure you have adb installed on the computer, USB debugging enabled on the phone, and the phone has allowed access to the computer. Plug the phone into the computer via USB cable, and make sure it's visible (it should show up in the Bash command adb devices.
In your computer's Chrome browser, open chrome://inspect/#devices, click the "Port forwarding" button, check "Enable port forwarding", and add the port on the computer that you want to be accessible from the phone (detailed instructions here). You'll need to keep open the tab running chrome://inspect/#devices.
In your phone's browser, navigate to localhost:[port_number], and it should display whatever is running on the computer.
This works on Windows and Ubuntu Linux, and should work on Mac as well.
How to Easily access LocalHost in Actual Android Device -> Connect your pc with the android device via USB
Go to Chrome inspection click 'f12' or Control+Shift+C
Chrome Inspection tool
Check the bottom of the chrome inspection tool.
Now go to settings in Remote Device Tab.
Remote Devices Tab
check on "Discover USB Device" option as well as check on "Port Forwarding" option.
Now Click on Add Rules, Enter Any Device Port e.g(4880) and in Local Address Enter the Actual Address of the local host in my case e.g (127.0.0.1:480)
After Adding the Rule go to your android studio -> inside your code URL(http://127.0.0.1:4880). Remember to change the port from 480 -> 4880.
Go to Remote Device Tab in Chrome and Click on your connected Device. Add New URL(127.0.0.1:4880)
Inspect the Android Device Chrome Browser
Check your Actual Device Chrome Browser and start Debugging the code on Actual Android device.
Check for the USB connection type options. You should have one called "Internet pass through". That will let your phone use the same connection as your PC.
I very much liked John's answer, but I'd like to give it with some changes to those that want to test some client//server configuration by running a client TCP on the USB connected Mobile and a server on the local PC.
First it's quite obvious that the 10.0.2.2 won't work because this is a REAL hardware mobile and not a simulator.
So Follow John's instructions:
Unplug all network cables on PC and turn off wifi.
Turn off wifi on the android device
Connect android device to pc via USB
Turn on the "USB Tethering" (USB Modem/ USB Cellular Modem / USB שימוש כמודם סלולרי) in the android menu. (Under networks->more...->Tethering and portable hotspot")
This USB connection will act as a DHCP server for you single PC connection, so it'll assign your PC a dedicated (dynamic) IP in its local USB network. Now all you have to do is tell the client application this IP and port.
Get the IP of your PC (that has been assigned by the USB tether cable.)
(open command prompt and type "ipconfig" then look for
the IP that the USB network adapter has assigned, in Linux its ifconfig or Ubuntu's "Connection information" etc..)
Tell the application to connect to that IP (i.e. 192.168.42.87) with something like (Java - client side):
String serverIP = "192.168.42.87";
int serverPort = 5544;
InetAddress serverAddress = InetAddress.getByName(serverIP);
Socket socket = new Socket(serverAddress, serverPort);
...
Enjoy..
Hello you can access your xampp localhost by
Control panel -->
windows defender firewall -->
Advance setting (on left side) --> Inbound Rules --> New Rule --> Port --> in specific local port write your Apache ports --> next --> next
then you can access your localhost by using local PC IP address:
Zsivics Sanel's answer above work fine for USB Debugging but if you want to access your localhost on device for Wifi ADB you can use command
adb reverse tcp:port tcp:port
and use the link http://localhost:port/ on your android to access the http://localhost:port/ of your pc.
This works for USB Debugging as well :)
The detailed answer with explaination is here.
Connecting android phone via USB cable and accessing http server on the Computer.
Firewall - switch it off, once everything works you can add a rule
under Inbound rules, enable Apache HTTP Server (UDP and TCP) (or whatever HTTP server you're using) Warning - if you switch off the firewall, your computer becomes vulnerable to internet attacks. So switch off internet and disconnect from network first. You don't need to be on the Internet for this to work, as you're connecting two devices on local network.
Plug in the USB cable. If networking window pops up, select Home Network. If it doesn't pop up it's probably set to Public (which won't work), so go to: Control Panel\Network and Internet\Network and Sharing Center (in win7) and set it to Home Network there. It might give you a next screen after you press it to share Pictures, Music... just click cancel on it. On the main screen Home Network will still be active.
You need the Computer's IP, localhost or other aliases don't work. To get the IP of your machine, start the Windows command line shell (press Windows Key + R and type cmd), the black command prompt window should pop up, type: "ipconfig" and hit enter. Now look for
IPv4 Address. . . . . . . . . . . : 192.168.###.###
That's the IP that your phone assigned to your computer
Now you should be done, in the phone's browser, try accessing a test html file from your computer's server it should display fine.
Now that everything is working you'll probably want to enable the firewall and Internet access on your computer. Now try accessing your http server again. It probably won't work. Depending on your setup you might get a popup asking you to add a rule to allow it, but if you don't, just add it yourself. Start the windows firewall and go to Inbound Rules (because it's a server) and add New Rule. Select "Program". Locate the exe of your http server, and in the next screen make sure Allow is selected. Try connecting again, and now it should work. If not it's probably because you selected wrong exe. You can even go more advanced to restrict outside usage (because now everyone on the internet can access it if they have your Internet IP address) To restrict access you can right-click on the new rule you've just created, click Properties, and go to Scope tab and change things there accordingly.
Note for Android developers, now in your android code, you have to use "http://", otherwise it won't work. For example: MyAsyncTask.execute("http://192.168.123.123/test.html")
Part II -- Setting IP to fixed/static.
Now that things work, every time you unplug the cable and plug it back in, your IP will change. And if you are using it somewhere like inside a code, you'll have to update it every time! Solution I've come up with is to set the IP to static.
Having the phone connected and tethered, go back to Network and Sharing Center and click on the Local Area connection "Connections:"
On the next screen select Properties, then highlight IP 4 and press properties there and click "Use the following IP address"
Put Default Gateway and Subnet Mask exactly as it was in the ipconfig, and for the IP Address, only change the last 3 digits to anything from 1 to 255
That IP will be your new fixed/static IP and it will remain the same next time you plug the phone.
Problem I faced maybe it will help some one, I was working long time where my pc and android device connected to the same WiFi network and the android device connected via the IPV4 address of the pc with opened port 8080 trought the fire wall.
ONE DAY :
I installed Genymotion emulator in order to debug UI on different screens.
THANKS to Genymotion/Virtualbox network configurations it ruined my WiFi adapter.
The fix was to reconfigure IPV4/TCP settings on the WiFi netwrok adapter :
Related
I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno
works well.
But when I connect my real Android phone, the phone browser can't connect to the same web server on my laptop. The phone is connected to the laptop using a USB cable. If I run the
adb devices command, I can see my phone.
What am I missing?
USB doesn't provide network to mobile device.
If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not 127.0.0.1).
To find out the IP address of your desktop:
type into the command line ipconfig (Windows) or ifconfig (Unix)
on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
there's a bunch of suggestions on how to have a similar output on Windows
there's going to be a bunch of IP's
try all of them (except the forementioned localhost and 127.0.0.1)
If your phone is connected to the mobile network, then things are going to be harder.
Either go hardcore:
first find out your router external IP address (https://www.google.de/search?q=myip)
then, on the router, forward some port to <your desktop IP>:<server port number>
finally use the external IP address and forwarded port
Otherwise use something like xip.io or ngrok.
NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:
ip address
The above ip command can be abbreviated to:
ip a
If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.
apt-get install net-tools
Reference is here
It is actually quite simple.
Turn on WiFi Hotspot of your Android phone/router and connect your
Laptop to your phone
Start your server at localhost (I am using WAMP server for Windows)
Now open the command prompt and enter
ipconfig
Once you've done that, you will see something like the following:
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
IPv4 Address. . . . . . . . . . . : 192.168.43.76
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.43.1
Copy the IPv4 Address (in this case, it is 192.168.43.76)
In your mobile browser, simply paste the IPv4 Address
Note: Please set your network as "Home Network". Setting the network as Home Network means that you are allowing your PC to share stuff with other devices on the same network.
If you are using Windows 10, this can be done with the following:
Open Settings
Go to Network & Internet
Select WiFi in the left menu
Tap on the name of the connected WiFi
Set the Network Profile of the network to be Private
If you are having an issue, it is most likely to do with Windows Firewall.
Open Control Panel
Go to Windows Defender Firewall
Tap on Allow an app or feature through Windows Defender Firewall
Check whether the app is enabled for Private networks (there should be a tick)
If it is not enabled, tap Change settings and tick the checkbox under Private for the app
With the simple solution (just access laptop_ip_addr:port from mobile device, when mobile and laptop are on the same WiFi), I get a ERR_CONNECTION_REFUSED error. That is, my MacBook seems to refuse the connection attempt from my mobile.
ADB Reverse Socket (Android only)
This solution works for me (tested with a MacBook):
Connect Android mobile device with USB cable to laptop
Enable USB Debugging on mobile device
On laptop, run adb reverse tcp:4000 tcp:4000
Use your custom port number instead of 4000
Now, on the mobile device, you can navigate to http://localhost:4000/, and it will actually connect to the laptop, not the mobile device
See instructions here.
The downside is that this works only with a single mobile device at a time. If you want access with another mobile device, you have to first disconnect the first one (disable USB Debugging), connect the new one (enable USB Debugging), and run adb reverse tcp:4000 tcp:4000 again.
EDIT: this solution has been reported to also work with Windows.
ngrok (works with all devices)
Another solution that should always work is ngrok (as mentioned in other answers). It works over the Internet, and not the local network.
It's extremely easy to use:
brew install ngrok
ngrok http 4000
This outputs, among some other information, a line like
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
Now, you can navigate to http://4cc5ac02.ngrok.io on any device that is connected to the Internet, and this URL redirects to localhost:4000 of your laptop.
Note that as long as the ngrok command is running (until you hit Ctrl-C), your project is publicly served. Everybody who has the URL can see it.
Mac OS X users
I achieved this by enabling remote management:
Ensure that your phone and laptop are connected to the same WiFi network
On Mac, go to System preferences/sharing
Enable remote management
You will see a message similar to this:
Other users can manage your computer using the address some.url.com
On your Android device, you should now be able to go to some.url.com, which delegates to localhost on your Mac. You can also use ifconfig to get the IP address of your Mac.
Portable solution with ngrok (any OS with Node.js)
If you don't mind exposing your project with a temporary domain you can use ngrok. Lets say I have an app that runs on localhost:9460 I can simply write
npm install ngrok -g
ngrok http 9460
This will give me:
Session Status online
Update update available (version 2.2.8, Ctrl-U to update)
Version 2.2.3
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://f7c23d14.ngrok.io -> localhost:9460
Forwarding https://f7c23d14.ngrok.io -> localhost:9460
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
I can now reach https://f7c23d14.ngrok.io as a way to remotely view localhost. This is great to share design work or progress with clients.
Alternate solution with nginx proxy pass
If you are running something like this through nginx proxy_pass it will require a bit more tweaking - this is a hacky approach, but it works for me and I am open to suggestions on improving it:
Enable remote management (as mentioned above)
Temporarily set the server to listen on port 81 as opposed to 80
Type in the following command:
sudo nginx -s reload
Visit http://youripaddress:81
server {
listen 80;
listen 81; # <-------- add this to expose the app on a unique port
server_name ~^(local|local\.m).example.com$;
# ...
}
Reload and visit http://youripaddress:81
This solution is usable even when your mobile device and computer is connected through usb:
You need to use port forwarding in this case.
In the Google chrome inspect window (chrome://inspect) You can see the connected devices.
Click on the port fowading button, and set a random port say 3000 to the port which your laptop uses say 8080.
now use localhost:3000 from device to access the localhost:8080(or_whatever_ip:portno) in the laptop.
You can check it in the mobile browser. Try localhost:3000 in the mobile browser.
Make sure to check the "Enable port forwarding" checkbox in the "Port forwarding settings" window
I found a quick solution to this problem. Try this link. It should help you fix the problem.
I only changed one thing, where the tutorial states you change '127.0.0.1' to 'All', change it to the IP address your server is running on instead.
After that, you should be able to connect to your localhost.
Below is a (proofread) copy of the information from the linked page:
Step 1
Install the Wamp server (or any other you prefer).
This is one of the best servers I know to set up a local server. If you have installed Apache or any other server, ignore this step.
Download and install Wamp Server from here.
Step 2
Add a new rule for port 80 in Windows Firewall.
Open the Control Panel and select Windows Firewall.
Select Advanced settings from the left panel of the Windows Firewall setting page.
Select Inbound Rules from the left panel, then select New Rule.
Select Port and click Next.
Select the “Specific local ports” radio button and enter 80 as the port value.
Keep Allow the connection unchanged and move to the next step.
Keep Profile options unchanged and click Next.
Give some nice name to your new rule and click Finish.
This will enable port 80 access on local network IP.
Step 3
Edit the httpd.conf file of the Wamp server to fix the 403 error.
We need to edit this file. Otherwise, we will get the 403 forbidden error when we access the localhost through a local network IP.
Click on the Wamp server tray icon.
Open Apache server sub menu.
Select httpd.conf.
Find this section of configuration in the httpd.conf file:
Directory “c:/wamp/www/”
#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride all
#
# Controls who can get stuff from this server.
#
# onlineoffline tag – don’t remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Find and replace ‘127.0.0.1’ with ‘All’, save the file, and restart your Wamp server.
Step 4
Find your local network IP.
Open the command prompt.
Type and enter the ipconfig command.
In my case, my local area network address is 10.0.0.2.
This is the IP which you need to access your localhost on your Android phone over wifi. To test if it is working, type this IP address in your desktop browser where your localhost server is installed. The browser should display your localhost page successfully. This will assure that this local network IP is now successfully accessible on your Android phone.
I hope this tutorial will help you to access your localhost over wifi.
On Windows PC You may not need to do anything else than finding out your IPv4 Address using "ipconfig" command.
Step 1 : Connect your phone to PC using USB cable.
Step 2 : Use command 'ipconfig' to find out your IPv4 Address (External IP of the Router) e.g 192.168.1.5 in my case.
Step 3: Just access "192.168.1.5:8080" from your phone browser. It works !
Other Env Details :
Windows 7, Google Nexus 4 (4.2.2), Tomcat Server, Grails Application.
You should also make sure that you have the permission to access internet in AndroidManifest file :
<uses-permission android:name="android.permission.INTERNET"/>
There is however a far better solution. You can access your host machine with the IP address "10.0.2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
As this is an old question, there is a new way to do this that is really really simple. Download the ADB Chrome Extension and then follow these instructions:
https://developers.google.com/chrome-developer-tools/docs/remote-debugging
"Port forwarding on Chrome for Android makes it easy to test your development site on mobile. It works by creating a listening TCP port on your mobile device that maps to a particular TCP port on your development machine. Traffic between these ports travels through USB, so the connection doesn't depend on your network configuration."
More details here: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding
Run CMD as administrator
and on CMD screen type ipconfig and the screen will appear with text
as this photo
and you can access your localhost using this ip
you have to be connected to same network as your pc connected to
Solution for Windows:
You are not able to view your website, mainly because your firewall (
default OS firewall or Antivirus firewall ) is preventing incoming
connections.
On Windows Firewall:
To allow incoming connections on specific ports, go to:
Control Panel > Windows Defender Firewall > Advanced Settings > Right
Click Inbound Rules > Add New Rule.
Then :
Select "Port" radio button.
Enter the specific port / range you on which you want to enable incoming connections ( the port on which your server is running )
Select "Allow the connection" radio button.
Click Next ( unless you want to change the defaults )
Give your rule a name and click Finish.
Note:
If you are using an Antivirus having a firewall, then the above steps will not work since the Antivirus would have disabled Windows firewall and instead runs its own firewall.
In that case, depending on the Antivirus program, go to your Antivirus's Firewall settings and open the port.
You may have your web server listening on your loopback interface and not on your network interface. Major signs of this are:
Hits on 127.0.0.1 and localhost (from localhost or Android emulator) work
Hits on 192.168.xxx.xxx do not work, whether from localhost, LAN, or WAN
I talk more about diagnosing this and fixing this in an answer here.
Try going to this file:
C:\wamp\bin\apache\Apache2.2.11\conf\httpd.conf
# onlineoffline tag - don't remove
Order Deny,Allow
Allow from all // change it Deny
Allow from 127.0.0.1
And change 10.0.2.2 to your IP address.
finally done in Ubuntu , i am running nodejs server on localhost:8080
1) open terminal type ifconfig
you will get ip something like this : inet addr:192.168.43.17
2) now simply put url address like this :
"192.168.43.17:8080" (8080 port coming from localhost port number)
ex : "192.168.43.17:8080/fetch"
Use this in your ubuntu/Macbook to get the ip address of your system. Your mobile and system should be in the same network
ip addr | grep inet This will give you an ip address which looks like 192.168.168.46. Use this in your smartphone.
Hope this helps.
If you are using a real device and still facing the problem, follow these steps as these steps help me to solve the issue:
Make sure both the mobile device and the laptop are on the same WiFi network.
verify your targeted URL has a port number, something like this:https://localhost:44301
run this command on the Android Studio terminal, adb reverse tcp:44301 tcp:44301
replace 44301 with your port number
Hopefully, you will be able to run without any issue.
Happy codding
I used this process:
Install Fiddler on the PC
Set up PC and Android device following these excellent instructions
Simply go to the browser on the Android device and type in http://ipv4.fiddler to access the localhost
Note that this process requires you to change the WiFi settings on the Android device at the start and end of every session. I found this less painful then rooting my Android device.
ngrok lets you put your localhost onto a temporary server and is very simple to set up. I've provided some steps here that can be found in the link:
Download the ngrok zip from the link above
Open the zip
Run your server locally and take note of the port number
In the terminal, go to the folder where ngrok lives and type ngrok http [port number]
You'll see a little dashboard in your terminal with an address pointing to your localhost. Point your app to that address and build to your device.
The easier way to default localhost is to use http://localhost:port. This works in a laptop and Android as well.
If it does not work, then in android set the default IP of your phone to 127.0.0.1:port :)
Open terminal and type :-
hostname -i
#127.0.0.1
hostname -I
#198.168.*.*
A solution to connect my mobile device to my wamp server based on my laptop:
First, wifi is not a router. So to connect my mobile device to my wamp server based on localhost on my laptop, I need a router.
I have downloaded and installed a free virtual router: https://virtualrouter.codeplex.com/
Configuring it is really simple:
right click on virtual router icon in System Tray
click on Configure virtual router
fill a password
if your internet connection is in ethernet, choose Shared connection : Ethernet
Then set wifi on on your laptop and device
On your device connect to the virtual router network name
Then you can connect to your laptop via your device by launching a browser and fill the IPV4 address of your laptop
(to find it on windows, type in cmd : ipconfig, and find ipv4 address)
You should see your wamp server home page.
Was running into this problem using several different localhost servers. Finally got my app up and running on the phone in seconds just by using the Python simple server. It only takes a few seconds so is worth a try before getting into any more complicated solutions. First, make sure you have Python installed. cmd+r and type python for Windows or $ python --version in mac terminal.
Next:
cd <your project root>
$ python -m SimpleHTTPServer 8000
Then just find the address of your host machine on the network, I used System Preferences/Sharing on mac to find it. Tap that into your Android device and should load your index.html and you should be good.
If not then the problem is something else and you may want to look into some of the other suggested solutions. Good luck!
* EDIT *
Another quick solution to try if you're using Chrome is the Web Server for Chrome extension. I found it a quick and super easy way to get access to localhost on my phone. Just make sure to check Accessible to local network under the Options and it should work on your cell without any problem.
This is what worked for me, I added another line after the 127.0.0.1 ip to specify the exact local network ip address (not the public ip address) of the device I wanted to use. In my case my Samsung Galaxy S3
As suggested by Bangptit edit the httpd.conf file (x being the version numbers):
C:\wamp\bin\apache\Apache2.x.x\conf\httpd.conf
Search for the onlineoffline tag and add the ip of your phone (I found my phones ip address in my routers configuration pages):
onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
my phones ip in the line below
Allow from 192.168.1.65
Allow from ::1
Allow from localhost
One could extend this to include an entire sub domain too for e.g. 192.168.1.0/24 etc
First of all connect your phone and computer to common wifi.
Then, open command prompt using run as administrator
Give ipconfig command
Which shows wireless lan ip
Use ip:port of your server to access in phone
EASIEST way (this worked flawlessly for me) is to locally host your site at 0.0.0.0:<port_no> and to access it using mobile devices, use <local_ipv4_address>:<port_no>/<path> in browser.
To know your local ipv4 address, just type ipconfig in cmd
ANY device connected to the SAME network can access this url.
Ngrok is the best solution.
If you're developing PHP then I recommend installing Laravel Valet, It has MacOS and Linux versions, then you may use valet share command. If you're developing any frontend tech and need to share a port like 3000 then use ngrok directly ngrok http 3000
Xampp & ngrok
You can also use ngrok with Xampp in Windows. You can use following process. It's quite easy and simple.
Sign up for a FREE account on ngrok
Download the ngrok file from Download section and Open it
Note the port your Apache server is running on. It's 80 in my case
In ngrok command line, enter ngrok http YOUR_PORT and copy the Forwarding address
Now you can use this Forwarding link on any device (i.e. your physical android devices) and it will be hosting your localhost server. For example,
Replace
http://localhost/my-files/
with
http://YOUR_FORWARDING_ADDRESS/my-files/
First of all make your machine(where server is running)IP address static. Enter this static IP address in the android code.
Next go to your wifi router's interface and check the left panel. You will see option such as port forwarding/forwarding. Click on it and forward port 80.
Now edit your htttpd.conf file and edit it for
Allow from All
. Restart your server. Everything should work fine now.
Additionally you can reserve the IP address of your machine so that it is always allocated to you.
In the left panel of your router's interface, find DHCP -> Address Reservation, click on it. Enter your machine's MAC address and the IP address you have entered in the code. Click on save. This will reserve the given IP address for your machine.
Adding a solution for future developers.
Copy address of your ip address. right click on your network -> network and sharing-> click on the connection you currently have-> details-> then the address beside ipv4 address is your ip address, note this down somewhere
Go to control panel -> system and security -> windows firewall -> advanced settings -> inbound rules -> new rules (follow the steps to add a port e.g 80, its really simple to follow)
put your ip address that you noted down on your phone browser and the port number you created the rule for beside it. e.g 192.168.0.2:80 and wala.
Possible solution if it doesn't connect.
right click network->open network and sharing-> look under view your active connections, under the name of your connection at the type of connection and click on it if it is public, and make sure to change it to a home network.
Although one answer has been accepted but this worked for me:
Make sure both your computer and android device are on same network.
get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.) (on windows, use `ipconfig)
Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it.
search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2**
try accessing 1**.1**.15*.2**:port/ from your mobile device.
It is as simple as this.
Building on Ilya Saunkin's answer, here's a command that should output your IP address formatted as a hyperlink - just substitute 3000 for whatever your port number is:
ifconfig | grep "inet " | grep -v 127.0.0.1 | sed 's/netmask.*//g' | sed 's/inet//g' | awk '{print "http://"$1":3000/"}'
Depending on where you're running the command, you should be able to (on a Mac) hold the command key and click/double-click to open the links.
I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno
works well.
But when I connect my real Android phone, the phone browser can't connect to the same web server on my laptop. The phone is connected to the laptop using a USB cable. If I run the
adb devices command, I can see my phone.
What am I missing?
USB doesn't provide network to mobile device.
If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not 127.0.0.1).
To find out the IP address of your desktop:
type into the command line ipconfig (Windows) or ifconfig (Unix)
on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
there's a bunch of suggestions on how to have a similar output on Windows
there's going to be a bunch of IP's
try all of them (except the forementioned localhost and 127.0.0.1)
If your phone is connected to the mobile network, then things are going to be harder.
Either go hardcore:
first find out your router external IP address (https://www.google.de/search?q=myip)
then, on the router, forward some port to <your desktop IP>:<server port number>
finally use the external IP address and forwarded port
Otherwise use something like xip.io or ngrok.
NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:
ip address
The above ip command can be abbreviated to:
ip a
If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.
apt-get install net-tools
Reference is here
It is actually quite simple.
Turn on WiFi Hotspot of your Android phone/router and connect your
Laptop to your phone
Start your server at localhost (I am using WAMP server for Windows)
Now open the command prompt and enter
ipconfig
Once you've done that, you will see something like the following:
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
IPv4 Address. . . . . . . . . . . : 192.168.43.76
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.43.1
Copy the IPv4 Address (in this case, it is 192.168.43.76)
In your mobile browser, simply paste the IPv4 Address
Note: Please set your network as "Home Network". Setting the network as Home Network means that you are allowing your PC to share stuff with other devices on the same network.
If you are using Windows 10, this can be done with the following:
Open Settings
Go to Network & Internet
Select WiFi in the left menu
Tap on the name of the connected WiFi
Set the Network Profile of the network to be Private
If you are having an issue, it is most likely to do with Windows Firewall.
Open Control Panel
Go to Windows Defender Firewall
Tap on Allow an app or feature through Windows Defender Firewall
Check whether the app is enabled for Private networks (there should be a tick)
If it is not enabled, tap Change settings and tick the checkbox under Private for the app
With the simple solution (just access laptop_ip_addr:port from mobile device, when mobile and laptop are on the same WiFi), I get a ERR_CONNECTION_REFUSED error. That is, my MacBook seems to refuse the connection attempt from my mobile.
ADB Reverse Socket (Android only)
This solution works for me (tested with a MacBook):
Connect Android mobile device with USB cable to laptop
Enable USB Debugging on mobile device
On laptop, run adb reverse tcp:4000 tcp:4000
Use your custom port number instead of 4000
Now, on the mobile device, you can navigate to http://localhost:4000/, and it will actually connect to the laptop, not the mobile device
See instructions here.
The downside is that this works only with a single mobile device at a time. If you want access with another mobile device, you have to first disconnect the first one (disable USB Debugging), connect the new one (enable USB Debugging), and run adb reverse tcp:4000 tcp:4000 again.
EDIT: this solution has been reported to also work with Windows.
ngrok (works with all devices)
Another solution that should always work is ngrok (as mentioned in other answers). It works over the Internet, and not the local network.
It's extremely easy to use:
brew install ngrok
ngrok http 4000
This outputs, among some other information, a line like
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
Now, you can navigate to http://4cc5ac02.ngrok.io on any device that is connected to the Internet, and this URL redirects to localhost:4000 of your laptop.
Note that as long as the ngrok command is running (until you hit Ctrl-C), your project is publicly served. Everybody who has the URL can see it.
Mac OS X users
I achieved this by enabling remote management:
Ensure that your phone and laptop are connected to the same WiFi network
On Mac, go to System preferences/sharing
Enable remote management
You will see a message similar to this:
Other users can manage your computer using the address some.url.com
On your Android device, you should now be able to go to some.url.com, which delegates to localhost on your Mac. You can also use ifconfig to get the IP address of your Mac.
Portable solution with ngrok (any OS with Node.js)
If you don't mind exposing your project with a temporary domain you can use ngrok. Lets say I have an app that runs on localhost:9460 I can simply write
npm install ngrok -g
ngrok http 9460
This will give me:
Session Status online
Update update available (version 2.2.8, Ctrl-U to update)
Version 2.2.3
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://f7c23d14.ngrok.io -> localhost:9460
Forwarding https://f7c23d14.ngrok.io -> localhost:9460
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
I can now reach https://f7c23d14.ngrok.io as a way to remotely view localhost. This is great to share design work or progress with clients.
Alternate solution with nginx proxy pass
If you are running something like this through nginx proxy_pass it will require a bit more tweaking - this is a hacky approach, but it works for me and I am open to suggestions on improving it:
Enable remote management (as mentioned above)
Temporarily set the server to listen on port 81 as opposed to 80
Type in the following command:
sudo nginx -s reload
Visit http://youripaddress:81
server {
listen 80;
listen 81; # <-------- add this to expose the app on a unique port
server_name ~^(local|local\.m).example.com$;
# ...
}
Reload and visit http://youripaddress:81
This solution is usable even when your mobile device and computer is connected through usb:
You need to use port forwarding in this case.
In the Google chrome inspect window (chrome://inspect) You can see the connected devices.
Click on the port fowading button, and set a random port say 3000 to the port which your laptop uses say 8080.
now use localhost:3000 from device to access the localhost:8080(or_whatever_ip:portno) in the laptop.
You can check it in the mobile browser. Try localhost:3000 in the mobile browser.
Make sure to check the "Enable port forwarding" checkbox in the "Port forwarding settings" window
I found a quick solution to this problem. Try this link. It should help you fix the problem.
I only changed one thing, where the tutorial states you change '127.0.0.1' to 'All', change it to the IP address your server is running on instead.
After that, you should be able to connect to your localhost.
Below is a (proofread) copy of the information from the linked page:
Step 1
Install the Wamp server (or any other you prefer).
This is one of the best servers I know to set up a local server. If you have installed Apache or any other server, ignore this step.
Download and install Wamp Server from here.
Step 2
Add a new rule for port 80 in Windows Firewall.
Open the Control Panel and select Windows Firewall.
Select Advanced settings from the left panel of the Windows Firewall setting page.
Select Inbound Rules from the left panel, then select New Rule.
Select Port and click Next.
Select the “Specific local ports” radio button and enter 80 as the port value.
Keep Allow the connection unchanged and move to the next step.
Keep Profile options unchanged and click Next.
Give some nice name to your new rule and click Finish.
This will enable port 80 access on local network IP.
Step 3
Edit the httpd.conf file of the Wamp server to fix the 403 error.
We need to edit this file. Otherwise, we will get the 403 forbidden error when we access the localhost through a local network IP.
Click on the Wamp server tray icon.
Open Apache server sub menu.
Select httpd.conf.
Find this section of configuration in the httpd.conf file:
Directory “c:/wamp/www/”
#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride all
#
# Controls who can get stuff from this server.
#
# onlineoffline tag – don’t remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Find and replace ‘127.0.0.1’ with ‘All’, save the file, and restart your Wamp server.
Step 4
Find your local network IP.
Open the command prompt.
Type and enter the ipconfig command.
In my case, my local area network address is 10.0.0.2.
This is the IP which you need to access your localhost on your Android phone over wifi. To test if it is working, type this IP address in your desktop browser where your localhost server is installed. The browser should display your localhost page successfully. This will assure that this local network IP is now successfully accessible on your Android phone.
I hope this tutorial will help you to access your localhost over wifi.
On Windows PC You may not need to do anything else than finding out your IPv4 Address using "ipconfig" command.
Step 1 : Connect your phone to PC using USB cable.
Step 2 : Use command 'ipconfig' to find out your IPv4 Address (External IP of the Router) e.g 192.168.1.5 in my case.
Step 3: Just access "192.168.1.5:8080" from your phone browser. It works !
Other Env Details :
Windows 7, Google Nexus 4 (4.2.2), Tomcat Server, Grails Application.
You should also make sure that you have the permission to access internet in AndroidManifest file :
<uses-permission android:name="android.permission.INTERNET"/>
There is however a far better solution. You can access your host machine with the IP address "10.0.2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
As this is an old question, there is a new way to do this that is really really simple. Download the ADB Chrome Extension and then follow these instructions:
https://developers.google.com/chrome-developer-tools/docs/remote-debugging
"Port forwarding on Chrome for Android makes it easy to test your development site on mobile. It works by creating a listening TCP port on your mobile device that maps to a particular TCP port on your development machine. Traffic between these ports travels through USB, so the connection doesn't depend on your network configuration."
More details here: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding
Run CMD as administrator
and on CMD screen type ipconfig and the screen will appear with text
as this photo
and you can access your localhost using this ip
you have to be connected to same network as your pc connected to
Solution for Windows:
You are not able to view your website, mainly because your firewall (
default OS firewall or Antivirus firewall ) is preventing incoming
connections.
On Windows Firewall:
To allow incoming connections on specific ports, go to:
Control Panel > Windows Defender Firewall > Advanced Settings > Right
Click Inbound Rules > Add New Rule.
Then :
Select "Port" radio button.
Enter the specific port / range you on which you want to enable incoming connections ( the port on which your server is running )
Select "Allow the connection" radio button.
Click Next ( unless you want to change the defaults )
Give your rule a name and click Finish.
Note:
If you are using an Antivirus having a firewall, then the above steps will not work since the Antivirus would have disabled Windows firewall and instead runs its own firewall.
In that case, depending on the Antivirus program, go to your Antivirus's Firewall settings and open the port.
You may have your web server listening on your loopback interface and not on your network interface. Major signs of this are:
Hits on 127.0.0.1 and localhost (from localhost or Android emulator) work
Hits on 192.168.xxx.xxx do not work, whether from localhost, LAN, or WAN
I talk more about diagnosing this and fixing this in an answer here.
Try going to this file:
C:\wamp\bin\apache\Apache2.2.11\conf\httpd.conf
# onlineoffline tag - don't remove
Order Deny,Allow
Allow from all // change it Deny
Allow from 127.0.0.1
And change 10.0.2.2 to your IP address.
finally done in Ubuntu , i am running nodejs server on localhost:8080
1) open terminal type ifconfig
you will get ip something like this : inet addr:192.168.43.17
2) now simply put url address like this :
"192.168.43.17:8080" (8080 port coming from localhost port number)
ex : "192.168.43.17:8080/fetch"
Use this in your ubuntu/Macbook to get the ip address of your system. Your mobile and system should be in the same network
ip addr | grep inet This will give you an ip address which looks like 192.168.168.46. Use this in your smartphone.
Hope this helps.
If you are using a real device and still facing the problem, follow these steps as these steps help me to solve the issue:
Make sure both the mobile device and the laptop are on the same WiFi network.
verify your targeted URL has a port number, something like this:https://localhost:44301
run this command on the Android Studio terminal, adb reverse tcp:44301 tcp:44301
replace 44301 with your port number
Hopefully, you will be able to run without any issue.
Happy codding
I used this process:
Install Fiddler on the PC
Set up PC and Android device following these excellent instructions
Simply go to the browser on the Android device and type in http://ipv4.fiddler to access the localhost
Note that this process requires you to change the WiFi settings on the Android device at the start and end of every session. I found this less painful then rooting my Android device.
ngrok lets you put your localhost onto a temporary server and is very simple to set up. I've provided some steps here that can be found in the link:
Download the ngrok zip from the link above
Open the zip
Run your server locally and take note of the port number
In the terminal, go to the folder where ngrok lives and type ngrok http [port number]
You'll see a little dashboard in your terminal with an address pointing to your localhost. Point your app to that address and build to your device.
The easier way to default localhost is to use http://localhost:port. This works in a laptop and Android as well.
If it does not work, then in android set the default IP of your phone to 127.0.0.1:port :)
Open terminal and type :-
hostname -i
#127.0.0.1
hostname -I
#198.168.*.*
A solution to connect my mobile device to my wamp server based on my laptop:
First, wifi is not a router. So to connect my mobile device to my wamp server based on localhost on my laptop, I need a router.
I have downloaded and installed a free virtual router: https://virtualrouter.codeplex.com/
Configuring it is really simple:
right click on virtual router icon in System Tray
click on Configure virtual router
fill a password
if your internet connection is in ethernet, choose Shared connection : Ethernet
Then set wifi on on your laptop and device
On your device connect to the virtual router network name
Then you can connect to your laptop via your device by launching a browser and fill the IPV4 address of your laptop
(to find it on windows, type in cmd : ipconfig, and find ipv4 address)
You should see your wamp server home page.
Was running into this problem using several different localhost servers. Finally got my app up and running on the phone in seconds just by using the Python simple server. It only takes a few seconds so is worth a try before getting into any more complicated solutions. First, make sure you have Python installed. cmd+r and type python for Windows or $ python --version in mac terminal.
Next:
cd <your project root>
$ python -m SimpleHTTPServer 8000
Then just find the address of your host machine on the network, I used System Preferences/Sharing on mac to find it. Tap that into your Android device and should load your index.html and you should be good.
If not then the problem is something else and you may want to look into some of the other suggested solutions. Good luck!
* EDIT *
Another quick solution to try if you're using Chrome is the Web Server for Chrome extension. I found it a quick and super easy way to get access to localhost on my phone. Just make sure to check Accessible to local network under the Options and it should work on your cell without any problem.
This is what worked for me, I added another line after the 127.0.0.1 ip to specify the exact local network ip address (not the public ip address) of the device I wanted to use. In my case my Samsung Galaxy S3
As suggested by Bangptit edit the httpd.conf file (x being the version numbers):
C:\wamp\bin\apache\Apache2.x.x\conf\httpd.conf
Search for the onlineoffline tag and add the ip of your phone (I found my phones ip address in my routers configuration pages):
onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
my phones ip in the line below
Allow from 192.168.1.65
Allow from ::1
Allow from localhost
One could extend this to include an entire sub domain too for e.g. 192.168.1.0/24 etc
First of all connect your phone and computer to common wifi.
Then, open command prompt using run as administrator
Give ipconfig command
Which shows wireless lan ip
Use ip:port of your server to access in phone
EASIEST way (this worked flawlessly for me) is to locally host your site at 0.0.0.0:<port_no> and to access it using mobile devices, use <local_ipv4_address>:<port_no>/<path> in browser.
To know your local ipv4 address, just type ipconfig in cmd
ANY device connected to the SAME network can access this url.
Ngrok is the best solution.
If you're developing PHP then I recommend installing Laravel Valet, It has MacOS and Linux versions, then you may use valet share command. If you're developing any frontend tech and need to share a port like 3000 then use ngrok directly ngrok http 3000
Xampp & ngrok
You can also use ngrok with Xampp in Windows. You can use following process. It's quite easy and simple.
Sign up for a FREE account on ngrok
Download the ngrok file from Download section and Open it
Note the port your Apache server is running on. It's 80 in my case
In ngrok command line, enter ngrok http YOUR_PORT and copy the Forwarding address
Now you can use this Forwarding link on any device (i.e. your physical android devices) and it will be hosting your localhost server. For example,
Replace
http://localhost/my-files/
with
http://YOUR_FORWARDING_ADDRESS/my-files/
First of all make your machine(where server is running)IP address static. Enter this static IP address in the android code.
Next go to your wifi router's interface and check the left panel. You will see option such as port forwarding/forwarding. Click on it and forward port 80.
Now edit your htttpd.conf file and edit it for
Allow from All
. Restart your server. Everything should work fine now.
Additionally you can reserve the IP address of your machine so that it is always allocated to you.
In the left panel of your router's interface, find DHCP -> Address Reservation, click on it. Enter your machine's MAC address and the IP address you have entered in the code. Click on save. This will reserve the given IP address for your machine.
Adding a solution for future developers.
Copy address of your ip address. right click on your network -> network and sharing-> click on the connection you currently have-> details-> then the address beside ipv4 address is your ip address, note this down somewhere
Go to control panel -> system and security -> windows firewall -> advanced settings -> inbound rules -> new rules (follow the steps to add a port e.g 80, its really simple to follow)
put your ip address that you noted down on your phone browser and the port number you created the rule for beside it. e.g 192.168.0.2:80 and wala.
Possible solution if it doesn't connect.
right click network->open network and sharing-> look under view your active connections, under the name of your connection at the type of connection and click on it if it is public, and make sure to change it to a home network.
Although one answer has been accepted but this worked for me:
Make sure both your computer and android device are on same network.
get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.) (on windows, use `ipconfig)
Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it.
search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2**
try accessing 1**.1**.15*.2**:port/ from your mobile device.
It is as simple as this.
Building on Ilya Saunkin's answer, here's a command that should output your IP address formatted as a hyperlink - just substitute 3000 for whatever your port number is:
ifconfig | grep "inet " | grep -v 127.0.0.1 | sed 's/netmask.*//g' | sed 's/inet//g' | awk '{print "http://"$1":3000/"}'
Depending on where you're running the command, you should be able to (on a Mac) hold the command key and click/double-click to open the links.
I have built an Android application using the emulator to test. It connects to localhost to get and send data which works well. Now I want to try that application on my mobile device. I have read that I have to:
connect my computer to my router network
connect my mobile to my router network
go to the code and replace the 10.0.2.2 with the IP of my computer
Am I right? If so, is that computer's IP the IP that connects my computer to internet?
If you are developing in Eclipse, all you have to do is:
Connect your device to your computer with a USB cable
Right click on the root of your app
Select "Run As" > Android Application
If your device is connected, it should automatically run
Note sometimes you have to select whether you run on your connected device or on your emulator, but a window will display your options and your just select one.
Simple as that.
You are correct, when you simply connect to your local router you can get to your localhost by changing the configuration to point to: "http://10.0.2.2:8080"
I found the other ways to be very tedious
http://juristr.com/blog/2009/10/accessing-host-machine-from-your/
http://developer.android.com/tools/devices/emulator.html#emulatornetworking
I want to debug my android application, I connect the android device to my PC using the USB cable. In my application there is a button to connect with localhost, ip for localhost is 10.0.2.2 and the port is 8080, I have read that when debugging on mobile, the ip 10.0.2.2 is the localhost for android device and not for my PC, so what changes should I make to the ip instead of 10.0.2.2? or do I have to make another change?
In this case my android device is sony ericsson xperia arc s.
Google has added support in Chrome 29 and higher to use reverse port forwarding to access a website hosted on your local development machine through the USB cable on Chrome for Android. Setup instructions can be found at the following URL:
Android Remote Debugging - Reverse Port Forwarding
Tools for Web Developers - Access Local Servers
As of desktop Chrome 30 Reverse Port Forwarding is no longer an experimental feature in Chrome. It can be accessed by typing about:inspect in the address bar of your PC, and by clicking the "Enable port forwarding" check box and clicking the "Configure port forwarding" button located to the top right of the window.
Once that is done, connect your mobile device via USB. Open Chrome on your mobile device to localhost:8000 (or whichever port you have configured on your local server).
The Reverse Port Forwarding functionality will make sure that your Android device now sees your PC's localhost.
As 10.0.2.2 is your system (pc)'s local host address (from emulator only). Actually android doesn't recognized localhost in url. so 10.0.2.2 is for that meant. Also for android device loopback address is 127.0.0.1.
Your url with 10.0.2.2 is correct. Also you can use Static IP of your system.
Just check for
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in your application's manifest file.
EDIT:
Here you are using port 8080 so, try with adb command on your host machine.
adb forward tcp:8080 tcp:8080
Also please elaborate on this line "i want to debugger my application on my mobile".
Note:
If you are going to test on real device use your Network IP of system (PC).
For that you need to make some changes in your xampp server... Assign 1 static IP address to your system and then you need to put your xampp server in online mode. after that you can use that ip address in your android application instead of 10.0.2.2. Works fine for me as i am using my localhost with my android application.
You can share your Mac's Internet connection over Wi-Fi. Then your Android app can connect to a Servlet running on the Mac with HTTP over Wi-Fi. The steps are:
Run System Preference on Mac
Goto "Sharing" tab
Turn on "Internet Sharing"
Select "Ethernet" in the "Share your connection from" combo
Select "Wi-Fi" in the "To Computers Using" list box
Use "Wi-Fi Options..." button to configure Wi-Fi security. Now your Mac is a Wi-Fi server, and it is sharing its Ethernet Internet connection.
Configure your Android device's Wi-Fi to connect to your Mac (in Settings command)
On your Mac, goto the Network tab in System Preferences, and select Wi-Fi in list to find out the IP address of your Mac on the Wi-Fi network (for me it was 169.254.66.223)
In your Android App you can now connect to the Servlet in your Mac with "http://169.254.66.223:8080/YourServer/YourServlet"
I think you have two options
The first one is using 10.0.2.3 when you use your real android device.it works for me.
Your Second opt is creating hotspot from your pc and connect your android device to the hotspot.
Find the ip address using cmd type "ipconfig" replace localhost with the ip address.
Thanks.
I'm able to access my laptop web server using the Android emulator, I'm using 10.0.2.2:portno
works well.
But when I connect my real Android phone, the phone browser can't connect to the same web server on my laptop. The phone is connected to the laptop using a USB cable. If I run the
adb devices command, I can see my phone.
What am I missing?
USB doesn't provide network to mobile device.
If both your desktop and phone are connected to the same WiFi (or any other local network), then use your desktop IP address assigned by the router (not localhost and not 127.0.0.1).
To find out the IP address of your desktop:
type into the command line ipconfig (Windows) or ifconfig (Unix)
on Linux the one-liner ifconfig | grep "inet " | grep -v 127.0.0.1 will yield only the important stuff
there's a bunch of suggestions on how to have a similar output on Windows
there's going to be a bunch of IP's
try all of them (except the forementioned localhost and 127.0.0.1)
If your phone is connected to the mobile network, then things are going to be harder.
Either go hardcore:
first find out your router external IP address (https://www.google.de/search?q=myip)
then, on the router, forward some port to <your desktop IP>:<server port number>
finally use the external IP address and forwarded port
Otherwise use something like xip.io or ngrok.
NOTE: The ifconfig command has been deprecated and thus missing by default on Debian Linux, starting from Debian stretch. The new and recommended alternative for examining a network configuration on Debian Linux is ip command. For example to use ip command to display a network configuration run the following:
ip address
The above ip command can be abbreviated to:
ip a
If you still prefer to use ifconfig as part of your daily sys admin routine, you can easily install it as part of the net-tools package.
apt-get install net-tools
Reference is here
It is actually quite simple.
Turn on WiFi Hotspot of your Android phone/router and connect your
Laptop to your phone
Start your server at localhost (I am using WAMP server for Windows)
Now open the command prompt and enter
ipconfig
Once you've done that, you will see something like the following:
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
IPv4 Address. . . . . . . . . . . : 192.168.43.76
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.43.1
Copy the IPv4 Address (in this case, it is 192.168.43.76)
In your mobile browser, simply paste the IPv4 Address
Note: Please set your network as "Home Network". Setting the network as Home Network means that you are allowing your PC to share stuff with other devices on the same network.
If you are using Windows 10, this can be done with the following:
Open Settings
Go to Network & Internet
Select WiFi in the left menu
Tap on the name of the connected WiFi
Set the Network Profile of the network to be Private
If you are having an issue, it is most likely to do with Windows Firewall.
Open Control Panel
Go to Windows Defender Firewall
Tap on Allow an app or feature through Windows Defender Firewall
Check whether the app is enabled for Private networks (there should be a tick)
If it is not enabled, tap Change settings and tick the checkbox under Private for the app
With the simple solution (just access laptop_ip_addr:port from mobile device, when mobile and laptop are on the same WiFi), I get a ERR_CONNECTION_REFUSED error. That is, my MacBook seems to refuse the connection attempt from my mobile.
ADB Reverse Socket (Android only)
This solution works for me (tested with a MacBook):
Connect Android mobile device with USB cable to laptop
Enable USB Debugging on mobile device
On laptop, run adb reverse tcp:4000 tcp:4000
Use your custom port number instead of 4000
Now, on the mobile device, you can navigate to http://localhost:4000/, and it will actually connect to the laptop, not the mobile device
See instructions here.
The downside is that this works only with a single mobile device at a time. If you want access with another mobile device, you have to first disconnect the first one (disable USB Debugging), connect the new one (enable USB Debugging), and run adb reverse tcp:4000 tcp:4000 again.
EDIT: this solution has been reported to also work with Windows.
ngrok (works with all devices)
Another solution that should always work is ngrok (as mentioned in other answers). It works over the Internet, and not the local network.
It's extremely easy to use:
brew install ngrok
ngrok http 4000
This outputs, among some other information, a line like
Forwarding http://4cc5ac02.ngrok.io -> localhost:4000
Now, you can navigate to http://4cc5ac02.ngrok.io on any device that is connected to the Internet, and this URL redirects to localhost:4000 of your laptop.
Note that as long as the ngrok command is running (until you hit Ctrl-C), your project is publicly served. Everybody who has the URL can see it.
Mac OS X users
I achieved this by enabling remote management:
Ensure that your phone and laptop are connected to the same WiFi network
On Mac, go to System preferences/sharing
Enable remote management
You will see a message similar to this:
Other users can manage your computer using the address some.url.com
On your Android device, you should now be able to go to some.url.com, which delegates to localhost on your Mac. You can also use ifconfig to get the IP address of your Mac.
Portable solution with ngrok (any OS with Node.js)
If you don't mind exposing your project with a temporary domain you can use ngrok. Lets say I have an app that runs on localhost:9460 I can simply write
npm install ngrok -g
ngrok http 9460
This will give me:
Session Status online
Update update available (version 2.2.8, Ctrl-U to update)
Version 2.2.3
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://f7c23d14.ngrok.io -> localhost:9460
Forwarding https://f7c23d14.ngrok.io -> localhost:9460
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
I can now reach https://f7c23d14.ngrok.io as a way to remotely view localhost. This is great to share design work or progress with clients.
Alternate solution with nginx proxy pass
If you are running something like this through nginx proxy_pass it will require a bit more tweaking - this is a hacky approach, but it works for me and I am open to suggestions on improving it:
Enable remote management (as mentioned above)
Temporarily set the server to listen on port 81 as opposed to 80
Type in the following command:
sudo nginx -s reload
Visit http://youripaddress:81
server {
listen 80;
listen 81; # <-------- add this to expose the app on a unique port
server_name ~^(local|local\.m).example.com$;
# ...
}
Reload and visit http://youripaddress:81
This solution is usable even when your mobile device and computer is connected through usb:
You need to use port forwarding in this case.
In the Google chrome inspect window (chrome://inspect) You can see the connected devices.
Click on the port fowading button, and set a random port say 3000 to the port which your laptop uses say 8080.
now use localhost:3000 from device to access the localhost:8080(or_whatever_ip:portno) in the laptop.
You can check it in the mobile browser. Try localhost:3000 in the mobile browser.
Make sure to check the "Enable port forwarding" checkbox in the "Port forwarding settings" window
I found a quick solution to this problem. Try this link. It should help you fix the problem.
I only changed one thing, where the tutorial states you change '127.0.0.1' to 'All', change it to the IP address your server is running on instead.
After that, you should be able to connect to your localhost.
Below is a (proofread) copy of the information from the linked page:
Step 1
Install the Wamp server (or any other you prefer).
This is one of the best servers I know to set up a local server. If you have installed Apache or any other server, ignore this step.
Download and install Wamp Server from here.
Step 2
Add a new rule for port 80 in Windows Firewall.
Open the Control Panel and select Windows Firewall.
Select Advanced settings from the left panel of the Windows Firewall setting page.
Select Inbound Rules from the left panel, then select New Rule.
Select Port and click Next.
Select the “Specific local ports” radio button and enter 80 as the port value.
Keep Allow the connection unchanged and move to the next step.
Keep Profile options unchanged and click Next.
Give some nice name to your new rule and click Finish.
This will enable port 80 access on local network IP.
Step 3
Edit the httpd.conf file of the Wamp server to fix the 403 error.
We need to edit this file. Otherwise, we will get the 403 forbidden error when we access the localhost through a local network IP.
Click on the Wamp server tray icon.
Open Apache server sub menu.
Select httpd.conf.
Find this section of configuration in the httpd.conf file:
Directory “c:/wamp/www/”
#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride all
#
# Controls who can get stuff from this server.
#
# onlineoffline tag – don’t remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Find and replace ‘127.0.0.1’ with ‘All’, save the file, and restart your Wamp server.
Step 4
Find your local network IP.
Open the command prompt.
Type and enter the ipconfig command.
In my case, my local area network address is 10.0.0.2.
This is the IP which you need to access your localhost on your Android phone over wifi. To test if it is working, type this IP address in your desktop browser where your localhost server is installed. The browser should display your localhost page successfully. This will assure that this local network IP is now successfully accessible on your Android phone.
I hope this tutorial will help you to access your localhost over wifi.
On Windows PC You may not need to do anything else than finding out your IPv4 Address using "ipconfig" command.
Step 1 : Connect your phone to PC using USB cable.
Step 2 : Use command 'ipconfig' to find out your IPv4 Address (External IP of the Router) e.g 192.168.1.5 in my case.
Step 3: Just access "192.168.1.5:8080" from your phone browser. It works !
Other Env Details :
Windows 7, Google Nexus 4 (4.2.2), Tomcat Server, Grails Application.
You should also make sure that you have the permission to access internet in AndroidManifest file :
<uses-permission android:name="android.permission.INTERNET"/>
There is however a far better solution. You can access your host machine with the IP address "10.0.2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
As this is an old question, there is a new way to do this that is really really simple. Download the ADB Chrome Extension and then follow these instructions:
https://developers.google.com/chrome-developer-tools/docs/remote-debugging
"Port forwarding on Chrome for Android makes it easy to test your development site on mobile. It works by creating a listening TCP port on your mobile device that maps to a particular TCP port on your development machine. Traffic between these ports travels through USB, so the connection doesn't depend on your network configuration."
More details here: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding
Run CMD as administrator
and on CMD screen type ipconfig and the screen will appear with text
as this photo
and you can access your localhost using this ip
you have to be connected to same network as your pc connected to
Solution for Windows:
You are not able to view your website, mainly because your firewall (
default OS firewall or Antivirus firewall ) is preventing incoming
connections.
On Windows Firewall:
To allow incoming connections on specific ports, go to:
Control Panel > Windows Defender Firewall > Advanced Settings > Right
Click Inbound Rules > Add New Rule.
Then :
Select "Port" radio button.
Enter the specific port / range you on which you want to enable incoming connections ( the port on which your server is running )
Select "Allow the connection" radio button.
Click Next ( unless you want to change the defaults )
Give your rule a name and click Finish.
Note:
If you are using an Antivirus having a firewall, then the above steps will not work since the Antivirus would have disabled Windows firewall and instead runs its own firewall.
In that case, depending on the Antivirus program, go to your Antivirus's Firewall settings and open the port.
You may have your web server listening on your loopback interface and not on your network interface. Major signs of this are:
Hits on 127.0.0.1 and localhost (from localhost or Android emulator) work
Hits on 192.168.xxx.xxx do not work, whether from localhost, LAN, or WAN
I talk more about diagnosing this and fixing this in an answer here.
Try going to this file:
C:\wamp\bin\apache\Apache2.2.11\conf\httpd.conf
# onlineoffline tag - don't remove
Order Deny,Allow
Allow from all // change it Deny
Allow from 127.0.0.1
And change 10.0.2.2 to your IP address.
finally done in Ubuntu , i am running nodejs server on localhost:8080
1) open terminal type ifconfig
you will get ip something like this : inet addr:192.168.43.17
2) now simply put url address like this :
"192.168.43.17:8080" (8080 port coming from localhost port number)
ex : "192.168.43.17:8080/fetch"
Use this in your ubuntu/Macbook to get the ip address of your system. Your mobile and system should be in the same network
ip addr | grep inet This will give you an ip address which looks like 192.168.168.46. Use this in your smartphone.
Hope this helps.
If you are using a real device and still facing the problem, follow these steps as these steps help me to solve the issue:
Make sure both the mobile device and the laptop are on the same WiFi network.
verify your targeted URL has a port number, something like this:https://localhost:44301
run this command on the Android Studio terminal, adb reverse tcp:44301 tcp:44301
replace 44301 with your port number
Hopefully, you will be able to run without any issue.
Happy codding
I used this process:
Install Fiddler on the PC
Set up PC and Android device following these excellent instructions
Simply go to the browser on the Android device and type in http://ipv4.fiddler to access the localhost
Note that this process requires you to change the WiFi settings on the Android device at the start and end of every session. I found this less painful then rooting my Android device.
ngrok lets you put your localhost onto a temporary server and is very simple to set up. I've provided some steps here that can be found in the link:
Download the ngrok zip from the link above
Open the zip
Run your server locally and take note of the port number
In the terminal, go to the folder where ngrok lives and type ngrok http [port number]
You'll see a little dashboard in your terminal with an address pointing to your localhost. Point your app to that address and build to your device.
The easier way to default localhost is to use http://localhost:port. This works in a laptop and Android as well.
If it does not work, then in android set the default IP of your phone to 127.0.0.1:port :)
Open terminal and type :-
hostname -i
#127.0.0.1
hostname -I
#198.168.*.*
A solution to connect my mobile device to my wamp server based on my laptop:
First, wifi is not a router. So to connect my mobile device to my wamp server based on localhost on my laptop, I need a router.
I have downloaded and installed a free virtual router: https://virtualrouter.codeplex.com/
Configuring it is really simple:
right click on virtual router icon in System Tray
click on Configure virtual router
fill a password
if your internet connection is in ethernet, choose Shared connection : Ethernet
Then set wifi on on your laptop and device
On your device connect to the virtual router network name
Then you can connect to your laptop via your device by launching a browser and fill the IPV4 address of your laptop
(to find it on windows, type in cmd : ipconfig, and find ipv4 address)
You should see your wamp server home page.
Was running into this problem using several different localhost servers. Finally got my app up and running on the phone in seconds just by using the Python simple server. It only takes a few seconds so is worth a try before getting into any more complicated solutions. First, make sure you have Python installed. cmd+r and type python for Windows or $ python --version in mac terminal.
Next:
cd <your project root>
$ python -m SimpleHTTPServer 8000
Then just find the address of your host machine on the network, I used System Preferences/Sharing on mac to find it. Tap that into your Android device and should load your index.html and you should be good.
If not then the problem is something else and you may want to look into some of the other suggested solutions. Good luck!
* EDIT *
Another quick solution to try if you're using Chrome is the Web Server for Chrome extension. I found it a quick and super easy way to get access to localhost on my phone. Just make sure to check Accessible to local network under the Options and it should work on your cell without any problem.
This is what worked for me, I added another line after the 127.0.0.1 ip to specify the exact local network ip address (not the public ip address) of the device I wanted to use. In my case my Samsung Galaxy S3
As suggested by Bangptit edit the httpd.conf file (x being the version numbers):
C:\wamp\bin\apache\Apache2.x.x\conf\httpd.conf
Search for the onlineoffline tag and add the ip of your phone (I found my phones ip address in my routers configuration pages):
onlineoffline tag - don't remove
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
my phones ip in the line below
Allow from 192.168.1.65
Allow from ::1
Allow from localhost
One could extend this to include an entire sub domain too for e.g. 192.168.1.0/24 etc
First of all connect your phone and computer to common wifi.
Then, open command prompt using run as administrator
Give ipconfig command
Which shows wireless lan ip
Use ip:port of your server to access in phone
EASIEST way (this worked flawlessly for me) is to locally host your site at 0.0.0.0:<port_no> and to access it using mobile devices, use <local_ipv4_address>:<port_no>/<path> in browser.
To know your local ipv4 address, just type ipconfig in cmd
ANY device connected to the SAME network can access this url.
Ngrok is the best solution.
If you're developing PHP then I recommend installing Laravel Valet, It has MacOS and Linux versions, then you may use valet share command. If you're developing any frontend tech and need to share a port like 3000 then use ngrok directly ngrok http 3000
Xampp & ngrok
You can also use ngrok with Xampp in Windows. You can use following process. It's quite easy and simple.
Sign up for a FREE account on ngrok
Download the ngrok file from Download section and Open it
Note the port your Apache server is running on. It's 80 in my case
In ngrok command line, enter ngrok http YOUR_PORT and copy the Forwarding address
Now you can use this Forwarding link on any device (i.e. your physical android devices) and it will be hosting your localhost server. For example,
Replace
http://localhost/my-files/
with
http://YOUR_FORWARDING_ADDRESS/my-files/
First of all make your machine(where server is running)IP address static. Enter this static IP address in the android code.
Next go to your wifi router's interface and check the left panel. You will see option such as port forwarding/forwarding. Click on it and forward port 80.
Now edit your htttpd.conf file and edit it for
Allow from All
. Restart your server. Everything should work fine now.
Additionally you can reserve the IP address of your machine so that it is always allocated to you.
In the left panel of your router's interface, find DHCP -> Address Reservation, click on it. Enter your machine's MAC address and the IP address you have entered in the code. Click on save. This will reserve the given IP address for your machine.
Adding a solution for future developers.
Copy address of your ip address. right click on your network -> network and sharing-> click on the connection you currently have-> details-> then the address beside ipv4 address is your ip address, note this down somewhere
Go to control panel -> system and security -> windows firewall -> advanced settings -> inbound rules -> new rules (follow the steps to add a port e.g 80, its really simple to follow)
put your ip address that you noted down on your phone browser and the port number you created the rule for beside it. e.g 192.168.0.2:80 and wala.
Possible solution if it doesn't connect.
right click network->open network and sharing-> look under view your active connections, under the name of your connection at the type of connection and click on it if it is public, and make sure to change it to a home network.
Although one answer has been accepted but this worked for me:
Make sure both your computer and android device are on same network.
get your computer's ip address. (in case of mac type ifconfig inside terminal and look for en0/en1 -> inet it should be something like 19*.16*.1.4.) (on windows, use `ipconfig)
Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e. 19*.16*.1.4 inside port enter your port. and save it.
search for what is my ip in google chrome, and get your ip, lets say it is 1**.1**.15*.2**
try accessing 1**.1**.15*.2**:port/ from your mobile device.
It is as simple as this.
Building on Ilya Saunkin's answer, here's a command that should output your IP address formatted as a hyperlink - just substitute 3000 for whatever your port number is:
ifconfig | grep "inet " | grep -v 127.0.0.1 | sed 's/netmask.*//g' | sed 's/inet//g' | awk '{print "http://"$1":3000/"}'
Depending on where you're running the command, you should be able to (on a Mac) hold the command key and click/double-click to open the links.