[英]Connecting to a local server from Android device
I am developing an Android app that connects to a Rails web server. 我正在开发一个连接到Rails Web服务器的Android应用程序。 I need to test the app against the development version of the server on my computer.
我需要针对计算机上服务器的开发版本测试该应用程序。 In order to work, the server needs incoming requests to be to the named domain.
为了正常工作,服务器需要将传入请求发送到命名域。
Requests are addressed to localsite.com:3000
. 请求被发送到
localsite.com:3000
。
For browser testing, the hosts file on my computer handles that: 为了进行浏览器测试,我计算机上的hosts文件将处理以下内容:
127.0.0.1 localsite.com
For testing from Android emulators, the hosts file in the emulated device handles that: 为了从Android模拟器进行测试,模拟设备中的hosts文件将处理以下内容:
10.0.2.2 localsite.com #Localhost from an Android emulator
Now I want to test it from a physical device (a Nexus6 that has developer tools enabled, but is not rooted. My preference is to keep it that way). 现在,我想从物理设备(启用了开发人员工具但未植根的Nexus6上进行测试)。我希望保持这种状态。 I can connect to my server from the phone by using the IP and port (
192.168.1.190:3000
), but then nothing on the server works, because it expects localsite.com:3000
. 我可以使用IP和端口(
192.168.1.190:3000
)从电话连接到我的服务器,但是随后服务器上的任何设备都localsite.com:3000
正常工作,因为它希望使用localsite.com:3000
。
Here are my options, as I understand them: 据我了解,这是我的选择:
Get the hosts file from the device: 从设备获取主机文件:
adb pull /system/etc/hosts ~/device_hosts/nexus6
Edit hosts file: 编辑主机文件:
192.168.1.190 localsite.com #IP of workstation on local network
Try to push hosts file back to device: 尝试将主机文件推送回设备:
adb push ~/device_hosts/nexus6/hosts /system/etc
This fails, with the error failed to copy: Read-only file system
. 这将失败,并且错误
failed to copy: Read-only file system
。
adb remount
also fails, as does adb root
: adbd cannot run as root in production builds
. adb remount
也会失败, adb root
也将失败: adbd cannot run as root in production builds
。
Question Part 1 : is there any way to edit the hosts file of the device without permanently rooting it? 问题第1部分 :有什么方法可以在不永久根目录的情况下编辑设备的hosts文件?
While Googling around, I have seen references to an alternative to the hosts file solution, which is to "run a DNS server on the Android phone". 在谷歌搜索时,我看到了替代主机文件解决方案的参考,该解决方案是“在Android手机上运行DNS服务器”。 It is far from clear to me how this could work to perform the same job as the hosts file.
对我来说,这还远远不能弄清如何执行与主机文件相同的工作。
I've installed the app DNS Server
on the phone, and am running a DNS server with a "Forward" rule, as follows: 我已经在电话上安装了应用程序
DNS Server
,并且正在按照“转发”规则运行DNS服务器,如下所示:
Server name: mysite
Port: 59953
Forward: mysite.com to 198.168.1.190
The result is absolutely nothing. 结果绝对没有。 Requests to mysite.com or mysite.com:3000 from the device do not reach the local server.
从设备到mysite.com或mysite.com:3000的请求未到达本地服务器。
Question Part 2 : Can I set up the DNS Server
app on my Android phone so that it can make requests to mysite.com:3000
running on a machine on the local network? 问题第二部分 :我可以在我的Android手机上设置
DNS Server
应用程序,以便它可以向运行在本地网络上的计算机上的mysite.com:3000
发出请求吗?
Bonus question : Are there any other approaches to testing on a local server from a physical, unrooted Android device that I have not yet considered? 额外的问题 :是否还有其他方法可以从我尚未考虑的物理,无根Android设备上在本地服务器上进行测试?
The solution that I've worked out for my Mac desktop/Android mobile/Rails server combo: 我为Mac桌面/ Android移动/ Rails服务器组合设计的解决方案:
Chrome://inspect
on the desktop to set up port forwarding for the mobile Chrome://inspect
为移动设备设置端口转发 This process is adapted from this resource: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding 此过程改编自以下资源: https : //developer.chrome.com/devtools/docs/remote-debugging#port-forwarding
Assumptions before you're at this point: 此时的假设:
chrome://inspect
chrome://inspect
I installed Squidman proxy server: http://squidman.net/squidman/index.html , running on port 8080. 我安装了Squidman代理服务器: http ://squidman.net/squidman/index.html,在端口8080上运行。
On the tab "Template", comment out this line to allow access to web apps running on the proxy host (eg, your localhost): 在“模板”选项卡上,注释掉以下行以允许访问在代理主机(例如,本地主机)上运行的Web应用程序:
# protect web apps running on the proxy host from external users
#http_access deny to_localhost
Start 'Squidman'. 启动“ Squidman”。 For troubleshooting, open the "Tools" menu to see the access logs.
要进行故障排除,请打开“工具”菜单以查看访问日志。
On Chrome on the desktop, open up chrome://inspect/#devices
. 在桌面设备上的Chrome上,打开
chrome://inspect/#devices
。 Select your device. 选择您的设备。 Click "Port Forwarding".
点击“端口转发”。 Enter a line to direct port 9000 to your proxy server:
输入一行以将端口9000定向到您的代理服务器:
9000 localhost:8080
On the device, open up the Wifi connection Advanced Settings. 在设备上,打开Wifi连接的“高级设置”。 Set it up as follows & save.
如下设置并保存。
Proxy: Manual
Proxy hostname: localhost
Proxy port: 9000
My Android device now connects correctly to localsite.com:3000
(as set up in the hosts file on the desktop)- this now works for me from the device browser, and from apps installed using adb install
. 我的Android设备现在可以正确连接到
localsite.com:3000
(在桌面上的hosts文件中进行了设置)-现在,该设备可以从设备浏览器以及通过adb install
应用程序中使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.