android connect to PC's localhost when debugger on mobile device - android

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.

Related

Connect to the PC's local host through usb cable on android device

I'm debuging an android application using my android smartphone (Sony Experia) and I need to connect to the computer's local host through the android app I'm developing. I tried several methods (USB tethering, Wifi hotspot) but I couldn't achieve it. Please support me with a solution. I know that simply 10.0.2.2 is working well on the android AVD but I've no idea about real devices.
I'm using Microsoft Internet Information Services (IIS) as my server and the web application is also running well. I don't know whether the firewall is blocking the port or not.
This should work:
Unplug all the network cables on the computer and turn off your wifi.
Turn off the wifi on your Xperia.
Connect your Xperia to your computer 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, It is Under 'Ethernet Adapter Local Area Connection' title. The Value for the IPv4 address is the One you need.)
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 it should work.
If it is working on your PC but not in your device, You have to allow your http through firewall.
Follow How to enable sharing of web site on localhost? to do it.

use computer to proxy android network

I am developing an Android app, and testing that on a real device.
I have a localhost server set up on my computer, and I want to the device to make request via my computer so that can access to this localhost environment in my computer.
I'm wondering if there is any tool in the adb shell that could allow me to proxy all network activity of the device by my computer, so it can access my localhost service?
NOTE: I know it works on emulator, I am just asking if this is possible on a real device.
Thank you
Install Fiddler on your computer, then on your android device connect to WiFi, long press your wifi configuration and choose modify network, then choose show advanced options and set the proxy field to your PC's IP address

worklight server root URL fial to connect in android device

I try to deploy a worklight application into my android device (HTC Desire), without any hope I get the following error:-
Request timeout for [ANDROID IP :8080/console/apps/services
Default options: on failure timed out for
http://ANDROIDIP:8080/console/apps/services...
Make sure the host address is available to the app (especially relevant for android and iphone apps]
I find my android IP address via whatip.com and I configure my application descriptor xml file with it.
it works with android emulator when I use the following
<worklightServerRootURL>http://10.0.2.2:8080</worklightServerRootURL>
but not with the mobile device
any advice?
worklightServerRootURL should point to the public IP address of the host machine, not to the device IP.
The device and Worklight Server must be in the same network (public Internet, wifi...).
10.0.2.2 is a special address that only works on the android emulator. On the emulator, it is routed to the host where the emulator is running. On an actual device, this address won't be routed anywhere.
As Idan said, the Worklight server must be reachable from the Android device. This usually means that either the Worklight server has an address that is reachable from the internet, or that the android device has a wifi connection to the LAN where the worklight server is running.
Assuming that you are trying to test using the test server in worklight studio, first determine your computer's IP address. If you are behind a NAT router, whatip.com returns the IP of the router, not of your computer. Use ifconfig (ipconfig on windows) at a command line to determine your computer's IP address. Your computer may have several IP addresses. It is important that you choose the ones that corresponds to the LAN where you will be connecting your Android device. If you are unsure which one this is, you may need to look at your router's configuration. Next, verify that your computer's firewall has port 8080 open. Then connect your android device via wifi to the same LAN as your computer.
To verify that your device can reach the worklight server, try opening the worklight console in the phone's browser. (http://[IP of computer]:8080/console) Once that works, you should be able to use the same IP in the worklightServerRootURL to build the app so that it will work on that device.

android connect to server instead of localhost from mobile instead of emulator

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

Accessing localhost of PC from USB connected Android mobile device

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 :

Categories

Resources