android在移动设备上调试器时连接到PC的本地主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11328554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android connect to PC's localhost when debugger on mobile device
提问by William Kinaan
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
.
我想调试我的 android 应用程序,我使用 USB 电缆将 android 设备连接到我的 PC。在我的应用程序中,有一个按钮可以连接本地主机,本地主机的 ip 是10.0.2.2
,端口是8080
,我读过在移动设备上调试时,ip10.0.2.2
是 android 设备的本地主机,而不是我的 PC,所以我应该做哪些更改到 ip 而不是10.0.2.2
? 还是我必须再做一次改变?在这种情况下,我的 android 设备是sony ericsson xperia arc s
.
采纳答案by user370305
As 10.0.2.2is your system (pc)'s local host address (from emulator only). Actually android doesn't recognized localhostin url. so 10.0.2.2is for that meant. Also for android device loopback address is 127.0.0.1.
因为10.0.2.2是您的系统 (pc) 的本地主机地址(仅来自模拟器)。实际上,android 无法识别url 中的localhost。所以10.0.2.2就是这个意思。同样对于 android 设备环回地址是127.0.0.1。
Your url with 10.0.2.2 is correct. Also you can use Static IPof your system.
您的 10.0.2.2 网址是正确的。您也可以使用系统的静态 IP。
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 commandon your host machine.
在这里,您使用的是 8080 端口,因此,请在主机上尝试使用adb 命令。
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 IPof system (PC).
如果您要在真实设备上进行测试,请使用系统 (PC) 的网络 IP。
回答by J.Hogan
Google has added support in Chrome 29 and higher to use reverse port forwardingto access a website hosted on your local development machine through the USB cableon Chrome for Android. Setup instructions can be found at the following URL:
Google 在 Chrome 29 及更高版本中添加了支持,以使用反向端口转发通过Android 版 Chrome 上的 USB 电缆访问托管在本地开发机器上的网站。可以在以下 URL 中找到设置说明:
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.
从桌面版 Chrome 30 开始,反向端口转发不再是 Chrome 中的一项实验性功能。可以通过about:inspect
在 PC 的地址栏中键入,然后单击“启用端口转发”复选框并单击位于窗口右上角的“配置端口转发”按钮来访问它。
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).
完成后,通过 USB 连接您的移动设备。在您的移动设备上打开 Chrome localhost:8000
(或您在本地服务器上配置的任何端口)。
The Reverse Port Forwarding functionality will make sure that your Android device now sees your PC's localhost
.
反向端口转发功能将确保您的 Android 设备现在可以看到您的 PC 的localhost
.
回答by Cool Compiler
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.
为此,您需要对您的 xampp 服务器进行一些更改...为您的系统分配 1 个静态 IP 地址,然后您需要将您的 xampp 服务器置于在线模式。之后,您可以在您的 android 应用程序中使用该 IP 地址,而不是 10.0.2.2。对我来说很好用,因为我在我的 android 应用程序中使用我的本地主机。
回答by Adam Gawne-Cain
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:
您可以通过 Wi-Fi 共享 Mac 的互联网连接。然后,您的 Android 应用程序可以通过 Wi-Fi 上的 HTTP 连接到在 Mac 上运行的 Servlet。步骤是:
- 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"
- 在 Mac 上运行系统偏好设置
- 转到“共享”选项卡
- 开启“互联网共享”
- 在“共享您的连接来自”组合中选择“以太网”
- 在“To Computers Using”列表框中选择“Wi-Fi”
- 使用“Wi-Fi 选项...”按钮配置 Wi-Fi 安全。现在,您的 Mac 是一台 Wi-Fi 服务器,并且正在共享其以太网 Internet 连接。
- 配置您的 Android 设备的 Wi-Fi 以连接到您的 Mac(在设置命令中)
- 在 Mac 上,转到“系统偏好设置”中的“网络”选项卡,然后在列表中选择“Wi-Fi”以找出 Mac 在 Wi-Fi 网络上的 IP 地址(对我来说是 169.254.66.223)
- 在您的 Android 应用程序中,您现在可以使用“http://169.254.66.223:8080/YourServer/YourServlet”连接到 Mac 中的 Servlet
回答by yosef girma
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.
第一个是在您使用真正的 android 设备时使用 10.0.2.3。它对我有用。
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.
使用 cmd 类型“ipconfig”查找 ip 地址,将 localhost 替换为 ip 地址。
Thanks.
谢谢。