可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)
回答1:
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.
回答2:
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
回答3:
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!
回答4:
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.
回答5:
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.
- 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, in windows's command prompt, type ipconfig and look for
IPv4 Address. . . . . . . . . . . : 192.168.###.###
That's the IP your phone gave 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.
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.
回答6:
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.
回答7:
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..
回答8:
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