【问题标题】:Get time of NTP-Server from Android App从 Android App 获取 NTP-Server 的时间
【发布时间】:2013-05-13 17:57:01
【问题描述】:

亲爱的安卓开发者,

我正在尝试在我的应用程序中实现 sntpclient 类,但它不起作用。

班级:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.3_r1/android/net/SntpClient.java

在我的代码中,我有以下几行:

public void onClickBtn(View v)
{
    SntpClient client = new SntpClient();
     if (client.requestTime("pool.ntp.org", 10)) {
         long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
         Toast.makeText(this, "Offset: " + now, Toast.LENGTH_LONG).show();
     }
} 

我真的不知道这种情况下网络超时是什么意思。

如果有人对我有任何想法或提示,那就太好了。

提前致谢!

【问题讨论】:

  • 有什么问题?会发生什么?
  • 什么都没有发生。我不知道我应该为“网络超时”输入什么,目前它是“10”...
  • 我不认为这可能是问题。您是否调试了应用程序并检查了它是否进入if 块?
  • 这就是我目前正在做的事情,请给我几分钟...
  • 它尝试了 if 块,但在那之后似乎没有任何反应,要求没有满足...... url 错误或“网络超时”可能在 android 清单中缺少某些东西?我已经实现了互联网和网络状态

标签: java android networking client ntp


【解决方案1】:

你应该像这样把它放在 AsyncTask 中:

class GetNTPAsynctask extends AsyncTask<String, Void, Boolean> {

@Override
        protected Boolean doInBackground(String... params) {
            private SntpClient sntpClient = new SntpClient();
            return sntpClient.requestTime("pool.ntp.org", 30000);
        }
}

Timeout:网络超时,以毫秒为单位。所以我用 30000 表示 30 秒。

【讨论】:

    【解决方案2】:

    您可以使用此完整示例:

    class getCurrentNetworkTime extends AsyncTask<String, Void, Boolean> {
    
        private Exception exception;
    
        protected Boolean doInBackground(String... urls) {
            NTPUDPClient timeClient = new NTPUDPClient();
            timeClient.setDefaultTimeout(3000);
            InetAddress inetAddress = null;
            boolean is_locale_date = false;
            try {
                inetAddress = InetAddress.getByName(G.TIME_SERVER);
                TimeInfo timeInfo = null;
                timeInfo = timeClient.getTime(inetAddress);
                long localTime = timeInfo.getReturnTime();
                long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
                if (new Date(localTime) != new Date(serverTime))
                    is_locale_date = true;
    
            } catch (UnknownHostException e) {
                e.printStackTrace();
                Log.e("UnknownHostException: ", e.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("IOException: ", e.getMessage());
            }
            return is_locale_date;
        }
    
        protected void onPostExecute(boolean local_date) {
            if(!local_date) {
                Log.e("Check ", "dates not equal" + local_date);
            }
        }
    }
    

    使用方法:

    new getCurrentNetworkTime().execute();
    

    【讨论】:

      【解决方案3】:

      检查你的清单是否有这个:

      <uses-permission android:name="android.permission.INTERNET" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多