2011-11-19 25 views
1

我使用下面的代碼來連接和從Android設備檢索從AtomicTime服務器UTC時間:獲得原子時間與Android

public static final String ATOMICTIME_SERVER="http://132.163.4.101:13"; 
BufferedReader in = null; 

try 
{ 
    URLConnection conn = new URL(ATOMICTIME_SERVER).openConnection(); 
    in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

    String atomicTime; 
    while (true) 
    { 
     if ((atomicTime = in.readLine()).indexOf("*") > -1) 
     { 
     break; 
     } 
    } 

    ... do something 
} 
catch ... 

它不返回任何數據。當從瀏覽器訪問的網址,我們得到如下:

55884 11-11-19 07:40:22 00 0 0 824.5 UTC(NIST)

誰能幫助?

回答

0

這是因爲TCP端口13上沒有HTTP服務。有白天服務。您應該使用Socket而不是URLConnection。或者也許爲Android找到一些NTP實現。

+0

感謝praetorian droid – user1051566

2
String atomicTime = ""; 
try 
{ 
    Socket socket = new Socket("132.163.4.101", 13); 
    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

    in.readLine(); // Ignore leading blank line 
    atomicTime = in.readLine(); 
    socket.close(); 
} 
catch.... 
+0

請問您能否介紹一段代碼說明? –