简体   繁体   English

从Android设备连接到本地服务器

[英]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: 据我了解,这是我的选择:

option 1: edit the hosts file of the device 选项1:编辑设备的主机文件

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文件?

option 2: use a DNS server on the Android phone 选项2:在Android手机上使用DNS服务器

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服务器组合设计的解决方案:

  1. Install a proxy server on the desktop, allow app connections 在桌面上安装代理服务器,允许应用程序连接
  2. Use Chrome://inspect on the desktop to set up port forwarding for the mobile 在桌面上使用Chrome://inspect为移动设备设置端口转发
  3. Set up Wifi on device to use proxy server 在设备上设置Wifi以使用代理服务器

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: 此时的假设:

  • You have a Developer Tools-enabled Android device that connects over USB to your desktop and shows up in chrome://inspect 您拥有启用了开发人员工具的Android设备,该设备通过USB连接到您的桌面,并显示在chrome://inspect
  • Your Android device & desktop machine are on the same Wifi network 您的Android设备和台式机位于同一Wifi网络上
  • You have set up the hosts file on your desktop computer so that you can locally access your server with the correct name 您已经在台式计算机上设置了主机文件,以便可以使用正确的名称在本地访问服务器

Step 1: Proxy server on the desktop: 步骤1:桌面上的代理服务器:

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. 要进行故障排除,请打开“工具”菜单以查看访问日志。

Step 2: Set up port forwarding for the device: 步骤2:为设备设置端口转发:

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

Step 3: Set up Wifi on device to use proxy server 步骤3:在设备上设置Wifi以使用代理服务器

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM