Android 主机文件使用

发布于 2024-10-02 03:12:47 字数 170 浏览 3 评论 0原文

我有以下问题:

我制作了一个自定义主机文件来测试我的应用程序的某些功能,然后将其推送到我的 android 模拟器。 问题是这些设置不会立即生效。我必须等待大约 10 分钟,他们才会开始活动。

所以我的问题是:如何使新的主机文件立即生效?我有很多不同的设置要测试,我不能每次都等 10 分钟。

I have the following problem:

I make a custom hosts file to test some features of my application and then push it to my android emulator.
The thing is that these settings do not take effect immediately. I have to wait about 10 minutes before they become active.

So my question is: how to make the new hosts file active instantly? I have many different settings to test and I can't wait 10 minutes every time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独陪着我 2024-10-09 03:12:47

Java 维护自己的内部 DNS 缓存。操作系统将立即反映新的主机文件(在命令行上使用 ping 进行验证),但您需要告诉 java 不要缓存任何内容。将这些行添加到您的测试应用程序中:

System.setProperty("networkaddress.cache.ttl" , "0");
System.setProperty("networkaddress.cache.male.ttl" , "0");

有关这些属性的更多信息,请参阅此处:
http://docs.oracle.com/javase/ 7/docs/technotes/guides/net/properties.html

Java maintains its own internal DNS cache. The operating system will reflect the new hosts file immediately (verify that with ping on the command line) but you'll need to tell java not to cache anything. Add these lines to your test application:

System.setProperty("networkaddress.cache.ttl" , "0");
System.setProperty("networkaddress.cache.negative.ttl" , "0");

For more information on these properties, see here:
http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html

裸钻 2024-10-09 03:12:47

我刚刚在已 root 的 Samsung Galaxy S 上编辑了主机文件,更改立即生效。也许您遇到的问题与亚行有关?

我这样做了:

  1. 使用 Busybox,使用 cp /etc/hosts /mnt/sdcard/hosts.new 将主机文件复制到可编辑位置;
  2. 使用预安装的文本编辑器编辑 /mnt/sdcard/hosts.new,添加我需要的两个条目。我使用了IP,然后是不合格的主机名,然后是FQDN,例如192.168.2.81 siva siva.myinventeddomain.org.au,但其他格式理论上也应该有效;
  3. 再次在 BusyBox 中,su 到 root;
  4. /system 默认情况下是 ro,所以我必须使用 mount -o remount,rw /system 将其设置为 rw;
  5. 为了节省稍后的输入,cd /etc(此时 shell 提示符显示 /system/etc 而不是 /etc,这让我怀疑符号链接恶作剧);
  6. 使用mvhostshosts.old备份默认的hosts文件(仅包含127.0.0.1 localhost);
  7. 使用mv /mnt/sdcard/hosts.newhosts安装新的hosts文件;
  8. 执行 sync (仅仅因为我很偏执 - 这不应该是必要的);
  9. 使用 mount -o remount,ro /system 重新挂载 /system fs ro;
  10. 退出BusyBox;
  11. 启动网络浏览器 (FireFox) 并在组合 URL/搜索字段中输入 sivasiva 是我添加的两个主机条目之一)。

在进行这些更改之前,第 11 步会导致 Google 愚蠢地搜索“siva”或其他内容;紧接着他们,我就得到了我的 LAN httpd 虚拟主机的首页,正如我所期望的那样。

从它工作到不工作,只用了不到 10 分钟的时间。

到 Sun 的 Java doco 的链接可能相关,也可能不相关(可能不相关)。
Android 根本不包含 Java VM,更不用说 Sun 的了。它运行一个名为 Dalvik 的不同 VM - 请参阅维基百科条目: http://en.wikipedia.org /wiki/Dalvik_%28software%29

事实上,你可以用一种看起来很像 Java 的语言对 Android 手机进行编程,但这并不是重点。

I just edited my hosts file on my rooted Samsung Galaxy S, and the changes took effect immediately. Perhaps the problem you're seeing is something to do with ADB?

I did this:

  1. Using Busybox, copy the hosts file to an editable location with cp /etc/hosts /mnt/sdcard/hosts.new;
  2. Edit /mnt/sdcard/hosts.new using the pre-installed text editor, adding the two entries I need. I used IP, then unqualified hostname, then FQDN, eg 192.168.2.81 siva siva.myinventeddomain.org.au, but other formats should in theory work too;
  3. In BusyBox again, su to root;
  4. /system is ro by default, so I had to make it rw with mount -o remount,rw /system;
  5. To save typing later, cd /etc (whereupon the shell prompt showed /system/etc rather than /etc, which makes me suspect symlink shenanigans);
  6. Back up the default hosts file (which contained only 127.0.0.1 localhost) with mv hosts hosts.old;
  7. Install new hosts file with mv /mnt/sdcard/hosts.new hosts;
  8. Execute sync (merely because I am paranoid - this shouldn't be necessary);
  9. Remount /system fs ro with mount -o remount,ro /system;
  10. Exit BusyBox;
  11. Fired up web browser (FireFox) and entered siva in the combined URL/search field thingy (siva being one of the two hosts entries I added).

Prior to these changes, step 11 resulted in a stupid Google search for 'siva' or something; immediately after them, I get my LAN httpd vhost's front page, as I expect.

There was well under 10 minutes elapsed between it working and it not working.

The link to Sun's Java doco may or may not be relevant (probably it isn't).
Android doesn't contain a Java VM at all, let alone Sun's one. It runs a different VM called Dalvik - see Wikipedia entry: http://en.wikipedia.org/wiki/Dalvik_%28software%29

The fact that you can program Android phones in a language that looks a lot like Java is beside the point.

一指流沙 2024-10-09 03:12:47

我还没有尝试过使用主机文件;但我尝试过使用自定义 DNS...

  1. 在某处启动 DNS 服务器(您可以加载 DD-WRT、拥有精美的路由器或在 PC 上运行守护程序)。
  2. 可选:使用正在运行的 DNS 服务器的位置配置本地路由器(DHCP 服务器)(使其位于 DNS 服务器列表中的第一个)。
  3. 如果跳过 #2:将您的手机配置为静态 IP(Wi-Fi 设置 -> 菜单按钮 -> 高级)。将 DNS1 设置为您的服务器。
  4. 让您的手机通过 WIFI 连接到该网络。

你完成了!现在,您可以管理 DNS 名称/IP 配对以及租用时间(例如,如果需要,电话将每 30 秒更新一次 IP)。有点复杂,但您不必在手机上加载主机文件:-)。

I haven't tried using the hosts file; but I have tried using custom DNS...

  1. Start a DNS server somewhere (you can load DD-WRT, have a fancy router, or run a daemon on your PC).
  2. OPTIONAL: Configure your local router (DHCP server) with the location of the running DNS server (make it first in the DNS server list).
  3. IF SKIPPING #2: Configure your phone for static IP (Wi-Fi Settings->Menu button->advanced). Set the DNS1 to be your server.
  4. Have your phone connect to this network over WIFI.

Your done! Now you can manage DNS Names/IP parings along with lease times (e.g. so the phone will update the IP every 30s if you need). Somewhat complicated, but you don't have to load the hosts file on the phone :-).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文