[英]Get real world time
I use this code for get real world time from internet: 我使用以下代码从互联网获取真实世界的时间:
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute( new HttpGet( "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo"));
StatusLine statusLine = httpResponse.getStatusLine();
if( statusLine.getStatusCode() == HttpStatus.SC_OK)
{
ByteArrayOutputStream byteTimeStamp = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo( byteTimeStamp);
byteTimeStamp.close();
String sTimeStamp = byteTimeStamp.toString(); // this data contains real world time, I now can parse it...
}
else
httpResponse.getEntity().getContent().close();
}
catch( Exception e) {}
This code work perfectly. 此代码完美地工作。 But it use yahoo-service and depends them availability. 但是它使用yahoo服务,并取决于它们的可用性。
I use second way with GPS-locations service, but this way work requires add permissions 我将GPS定位服务与第二种方式结合使用,但是这种方式需要添加权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
This is not good idea. 这不是一个好主意。
I need another way for get real world time. 我需要另一种获取真实世界时间的方法。 Any ideas please. 有什么想法请。
Also I need get world time with millisecounds, if it possible. 如果可能的话,我还需要用毫厘秒获得世界时间。
One possibility is using Apache software, for example for DAYTIME protocol DaytimeUDPClient . 一种可能是使用Apache软件,例如用于DAYTIME协议DaytimeUDPClient 。 Then you can interprete the output by help of SimpleDateFormat
. 然后,您可以借助SimpleDateFormat
解释输出。 Another more precise solution would be based on SNTP. 另一个更精确的解决方案将基于SNTP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.