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.
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
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, eg 192.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 with mount -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
) with mv hosts hosts.old
;mv /mnt/sdcard/hosts.new hosts
;sync
(merely because I am paranoid - this shouldn't be necessary);/system
fs ro with mount -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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With