Java TrueTime有时不提供价值
我的应用程序中有一个工具。它的工作原理很好,但有时当我打开我的应用程序,我没有得到的价值。我已经制定了每日奖励方法,但如果真的发生了,这将是一个大问题 下面是代码,通过它我可以获得时间:Java TrueTime有时不提供价值,java,android,time,ntp,current-time,Java,Android,Time,Ntp,Current Time,我的应用程序中有一个工具。它的工作原理很好,但有时当我打开我的应用程序,我没有得到的价值。我已经制定了每日奖励方法,但如果真的发生了,这将是一个大问题 下面是代码,通过它我可以获得时间: class GoogleTime extends AsyncTask<Void,Void,Void> { @SuppressLint("CheckResult") @Override protected Void doInBackground(Void
class GoogleTime extends AsyncTask<Void,Void,Void> {
@SuppressLint("CheckResult")
@Override
protected Void doInBackground(Void... voids) {
try {
TrueTime.build().initialize();
TrueTimeRx.build()
.initializeRx("time.google.com")
.subscribeOn(Schedulers.io())
.subscribe(date -> {
java.util.Date da = TrueTimeRx.now();
Log.w("PPPPP", "TrueTime was initialized and we have a time: " + da);
String mm= String.valueOf(da);
String name = mm.substring(0, mm.lastIndexOf("GMT+06:00"));
String yyy = mm.substring(mm.length()-4);
String f = name+yyy;
String []strArray=f.split(" ");
Month = getMonthInIntFormat(strArray[1]);
String dd = strArray[2];
String yy = strArray[4];
String Datw = dd +""+Month+""+yy;
TotalDate =Integer.parseInt(Datw);
Log.w("PPPPP", "TrueTime was initialized and we have a time: " + TotalDate);
}
, Throwable::printStackTrace);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
类GoogleTime扩展异步任务{
@SuppressLint(“检查结果”)
@凌驾
受保护的空位背景(空位…空位){
试一试{
TrueTime.build().initialize();
TrueTimeRx.build()
.initializeRx(“time.google.com”)
.subscribeOn(Schedulers.io())
.订阅(日期->{
java.util.Date da=TrueTimeRx.now();
w(“PPPPP”,“TrueTime已初始化,我们有一个时间:”+da);
字符串mm=字符串的值(da);
字符串名称=mm.子字符串(0,mm.lastIndexOf(“GMT+06:00”);
字符串yyy=mm.子字符串(mm.长度()-4);
字符串f=name+yyy;
字符串[]strArray=f.split(“”);
月份=getMonthInIntFormat(strArray[1]);
字符串dd=strArray[2];
字符串yy=strArray[4];
字符串Datw=dd+“”+Month+“”+yy;
TotalDate=Integer.parseInt(Datw);
Log.w(“PPPPP”,“TrueTime已初始化,我们有一个时间:“+TotalDate”);
}
,Throwable::printStackTrace);
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
}
我已经在create上执行了这个类。我在
.subscribe(date->{
上也遇到了一个错误。它说的Single.subscribe()的结果被忽略。使用HttpGet、Client和Response,我设法从Response date头中获取服务器的当前时间。我可以随时调用它,并获得可靠的响应(谷歌几乎100%可用,我可以相信它能获得正确的日期和时间)
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
String dateStr = response.getFirstHeader("Date").getValue();
//Here I do something with the Date String
System.out.println(dateStr);
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch (ClientProtocolException e) {
Log.d("Response", e.getMessage());
}catch (IOException e) {
Log.d("Response", e.getMessage());
}