TrueTime 有时不给值
TrueTime Sometimes not giving value
我在我的应用程序中实现了 Truetime。它工作正常,但有时当我打开我的应用程序时,我没有得到价值。我已经设置了每天的奖励方法,但是如果发生了那将是一个大问题。
这是代码,我通过它获得时间:
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;
}
}
我已经在创建时执行了 class。我在 .subscribe(date -> {
上也遇到了错误。它的说法 Resultof Single.subscribe() 被忽略了。
使用 HttpGet、客户端和响应,我设法从响应日期 Header 获取服务器的当前时间。我可以随时调用它,并且会得到自信的响应(Google 几乎 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());
}
我在我的应用程序中实现了 Truetime。它工作正常,但有时当我打开我的应用程序时,我没有得到价值。我已经设置了每天的奖励方法,但是如果发生了那将是一个大问题。
这是代码,我通过它获得时间:
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;
}
}
我已经在创建时执行了 class。我在 .subscribe(date -> {
上也遇到了错误。它的说法 Resultof Single.subscribe() 被忽略了。
使用 HttpGet、客户端和响应,我设法从响应日期 Header 获取服务器的当前时间。我可以随时调用它,并且会得到自信的响应(Google 几乎 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());
}