0

I have implement Truetime in my app. It's works fine but sometimes when i open my app i am not getting the value . I have set daily reward method but if it happens it will be a big problem .

Here is the code , by which i am getting time :

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;
        }


    }

I have execute the class on create . I am also getting a error on .subscribe(date -> { this. Its saying Resultof Single.subscribe() is ignored.

4

1 回答 1

1

Using the HttpGet, Client and Response, I manage to get a server's current time from the response Date Header. I can call this all the times I want and will get confident responses (Google is almost 100% available and I can trust on getting correct Date and Time)

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());
    }
于 2020-04-16T19:41:27.830 回答