3

I'm trying to get the atomic time for an Android app. I'm using http://hi-android.info/src/android/net/SntpClient.java.html as NTP client and the following code to implement the NTP (= sample code from SntpClient.java):

SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
}

I tried different server from this list: http://mindprod.com/jgloss/timesources.html, but the "client.requestTime("SERVER")" is always "false" ...

My code:

SntpClient client = new SntpClient();
if (client.requestTime("hera.limescope.net", 10000)) {
    long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
    textAtomicClock.setText(String.valueOf(now));
}

What am I doing wrong?

Freddi

Freddi
  • 41
  • 5
  • You said you tried several server, but as a note the one you have above does not have a correctly configured DNS. Try `0.debian.pool.ntp.org` and you should get a working one. – Qben Sep 04 '13 at 07:41
  • That's just an example. I'm located in Germany so I tried every DE Server listed ... – Freddi Sep 04 '13 at 07:53
  • I just tried a random number (not that many) from the list and none answered on port 123. Anyway, it was just a comment. :) – Qben Sep 04 '13 at 08:13
  • I now tried `client.requestTime("0.debian.pool.ntp.org", 1000000)`, too. Didn't work. Perhaps it's a permission problem? Does a socket connection need a permission in my Manifest.xml? – Freddi Sep 04 '13 at 08:24
  • Yes, you need permission to access internet. ``. – Qben Sep 04 '13 at 08:55
  • You should add logging to the `SntpClient` and check `logcat` what exceptions you get. – Qben Sep 04 '13 at 08:58
  • I already had an INTERNET permission ... maybe I need a special one for a socket connection?! I also checked the logcat and got this: `09-04 20:29:09.014: E/Trace(16328): error opening trace file: No such file or directory` ... – Freddi Sep 04 '13 at 18:34
  • Maybe it's an sdk problem. I use `` and I can't find any sdk requirements listed in the SntpClient.java ... – Freddi Sep 04 '13 at 19:28
  • Check this for `error opening....` http://stackoverflow.com/questions/11446049/error-opening-trace-file-no-such-file-or-directory-2 – Qben Sep 05 '13 at 05:19
  • Changed nothing, it's still not working :( maybe the mistake is somewhere else ... so I uploaded my code: [link](http://www.xup.in/dl,50306143/Dashboard_2013-09-08.zip/) maybe somebody will have a look ... – Freddi Sep 08 '13 at 12:35
  • One thing came to my mind that I assume you already checked, but do you run this in the emulator? Have you verified that the emulator have Internat access? – Qben Sep 09 '13 at 06:28
  • The app is tested on a tablet, which has a working wifi connection. I also tested, if the app really has an internet connection. – Freddi Sep 09 '13 at 09:08
  • Found my mistake! I did know that I had to process the ntp task in the background ... I solved the problem with Androids AsyncTask function. Thanks to all who helped me! – Freddi Sep 18 '13 at 18:02

1 Answers1

1

Found my mistake! I didn't know that I had to process the ntp task in the background ... I solved the problem with Androids AsyncTask function. Thanks to all who helped me!

Freddi
  • 41
  • 5