java – 如何从Android中的网络中获取时间
作者:互联网
在我的应用程序中,我想使用网络时间而不是设备的时间.我的应用程序的要求是时间应该是正确的.
我正试图从NTS服务器获得时间,但加载器继续运行并且不停止我等待超过30分钟,但我仍然没有得到任何东西.
我想知道是否有任何其他方法来获取来自网络,因为我没有从网络中获取时间花费这么多时间.
public class MainActivity extends Activity {
public static final String TIME_SERVER = "time-a.nist.gov";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnGetTime = (Button)findViewById(R.id.button1);
btnGetTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
new GetTimeFromNetwork().execute();
}
});
}
public class GetTimeFromNetwork extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
Log.i( "pre execute","yes");
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.i("In Get time From Server class", "yes");
try {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
//long returnTime = timeInfo.getReturnTime(); //local device time
long returnTime = timeInfo.getMessage().getTransmitTimeStamp()
.getTime(); //server time
Date time = new Date(returnTime);
Log.i("time", "Time from " + TIME_SERVER + ": " + time);
} catch (Exception e) {
// TODO: handle exception
Log.e("error",e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
} }
}
解决方法:
这个解决方案对我有用:
在你的模块中导入gradle这个依赖(也许你已经拥有它):
compile 'commons-net:commons-net:3.3'
这里是获取本地/净时间的示例.如您所知,您应该像在解决方案中一样在异步任务中运行它.
public static final String TIME_SERVER = "time-a.nist.gov";
public static void printTimes() throws IOException {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
//long returnTime = timeInfo.getReturnTime(); //local device time
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); //server time
Date time = new Date(returnTime);
Log.e("getCurrentNetworkTime", "Time from " + TIME_SERVER + ": " + time);
Log.e("Local time", "Local time");
Log.e("Local time", "Current time: " + new Date(System.currentTimeMillis()));
Log.e("Local time", "Time info: " + new Date(timeInfo.getReturnTime()));
Log.e("Local time", "GetOriginateTimeStamp: " + new Date(timeInfo.getMessage().getOriginateTimeStamp().getTime()));
Log.e("NTP time", "Time from " + TIME_SERVER + ": " + time);
Log.e("Local time", "Time info: " + new Date(timeInfo.getMessage().getReceiveTimeStamp().getTime()));
Log.e("Local time", "GetOriginateTimeStamp: " + new Date(timeInfo.getMessage().getTransmitTimeStamp().getTime()));
}
总结只获得您的净时间:
public static long getNetworkTime() throws IOException {
NTPUDPClient timeClient = new NTPUDPClient();
InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getMessage().getReceiveTimeStamp().getTime();
}
我希望这有帮助!!
标签:android-networking,android,java 来源: https://codeday.me/bug/20190824/1709502.html