Android 主机文件使用
我有以下问题:
我制作了一个自定义主机文件来测试我的应用程序的某些功能,然后将其推送到我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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
我刚刚在已 root 的 Samsung Galaxy S 上编辑了主机文件,更改立即生效。也许您遇到的问题与亚行有关?
我这样做了:
/mnt/sdcard/hosts.new
,添加我需要的两个条目。我使用了IP,然后是不合格的主机名,然后是FQDN,例如192.168.2.81 siva siva.myinventeddomain.org.au
,但其他格式理论上也应该有效;su
到 root;/system
默认情况下是 ro,所以我必须使用mount -o remount,rw /system
将其设置为 rw;cd /etc
(此时 shell 提示符显示/system/etc
而不是/etc
,这让我怀疑符号链接恶作剧);mvhostshosts.old
备份默认的hosts文件(仅包含127.0.0.1 localhost
);mv /mnt/sdcard/hosts.newhosts
安装新的hosts文件;sync
(仅仅因为我很偏执 - 这不应该是必要的);mount -o remount,ro /system
重新挂载/system
fs ro;siva
(siva
是我添加的两个主机条目之一)。在进行这些更改之前,第 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:
cp /etc/hosts /mnt/sdcard/hosts.new
;/mnt/sdcard/hosts.new
using the pre-installed text editor, adding the two entries I need. I used IP, then unqualified hostname, then FQDN, eg192.168.2.81 siva siva.myinventeddomain.org.au
, but other formats should in theory work too;su
to root;/system
is ro by default, so I had to make it rw withmount -o remount,rw /system
;cd /etc
(whereupon the shell prompt showed/system/etc
rather than/etc
, which makes me suspect symlink shenanigans);127.0.0.1 localhost
) withmv hosts hosts.old
;mv /mnt/sdcard/hosts.new hosts
;sync
(merely because I am paranoid - this shouldn't be necessary);/system
fs ro withmount -o remount,ro /system
;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.
我还没有尝试过使用主机文件;但我尝试过使用自定义 DNS...
你完成了!现在,您可以管理 DNS 名称/IP 配对以及租用时间(例如,如果需要,电话将每 30 秒更新一次 IP)。有点复杂,但您不必在手机上加载主机文件:-)。
I haven't tried using the hosts file; but I have tried using custom DNS...
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 :-).