TrueTimeRx无效,请求时间错误

时间:2019-04-10 12:01:08

标签: java android runtime

我将TrueTimeRx用于以下代码的android studio项目:

public void TrueTimeFunction()
{
    TrueTimeRx.build()
            .withConnectionTimeout(31428)
            .withRetryCount(100)
            .withSharedPreferences(this)
            .withLoggingEnabled(true)
            .initializeRx("time.google.com")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<Date>() {
                @Override
                public void accept(Date date) {
                    if (!TrueTimeRx.isInitialized()) {
                        Log.d(TAG, "Sorry TrueTime not yet initialized");
                        return;
                    }

                    Date trueTime = TrueTimeRx.now();
                    Log.d(TAG, "Date: " + trueTime);
                    //rr.child("hahalol").setValue("aa"+date.toString());
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) {
                    Log.d(TAG, "Error: ", throwable);
                }
            }, new Action() {
                @Override
                public void run() {
                    Log.d(TAG, "Completed!");
                }
            });

}

我这样调用函数:

TrueTimeFunction();
Date date = TrueTimeRx.now();

我遇到以下错误:

E/TrueTimeRx: ---- Error requesting time
java.net.SocketTimeoutException
    at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:601)
    at libcore.io.IoBridge.recvfrom(IoBridge.java:559)
    at java.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:163)
    at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:171)
    at java.net.DatagramSocket.receive(DatagramSocket.java:274)
    at com.instacart.library.truetime.SntpClient.requestTime(SntpClient.java:123)
    at com.instacart.library.truetime.TrueTime.requestTime(TrueTime.java:122)
    at com.instacart.library.truetime.TrueTimeRx$4$1$2.subscribe(TrueTimeRx.java:197)
    at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:72)
    at io.reactivex.Flowable.subscribe(Flowable.java:14349)
    at io.reactivex.Flowable.subscribe(Flowable.java:14295)
    at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
 Caused by: android.system.ErrnoException: recvfrom failed: EAGAIN (Try again)
    at libcore.io.Posix.recvfromBytes(Native Method)
    at libcore.io.Posix.recvfrom(Posix.java:161)
    at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
    at libcore.io.IoBridge.recvfrom(IoBridge.java:556)
    at java.net.PlainDatagramSocketImpl.doRecv(PlainDatagramSocketImpl.java:163) 
    at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:171) 
    at java.net.DatagramSocket.receive(DatagramSocket.java:274) 
    at com.instacart.library.truetime.SntpClient.requestTime(SntpClient.java:123) 
    at com.instacart.library.truetime.TrueTime.requestTime(TrueTime.java:122) 
    at com.instacart.library.truetime.TrueTimeRx$4$1$2.subscribe(TrueTimeRx.java:197) 
    at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:72) 
    at io.reactivex.Flowable.subscribe(Flowable.java:14349) 
    at io.reactivex.Flowable.subscribe(Flowable.java:14295) 
    at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82) 
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) 
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    at java.lang.Thread.run(Thread.java:818) 

我试图避免使用函数,而我直接这样做了

TrueTimeRx.build()
                    .withConnectionTimeout(31428)
                    .withRetryCount(100)
                    .withSharedPreferences(Start.this)
                    .withLoggingEnabled(true)
                    .initializeRx("time.google.com")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<Date>() {
                        @Override
                        public void accept(Date date) {
                            if (!TrueTimeRx.isInitialized()) {
                                Log.d(TAG, "Sorry TrueTime not yet initialized");
                                return;
                            }

                            Date trueTime = TrueTimeRx.now();
                            Log.d(TAG, "Date: " + trueTime);
                            //rr.child("hahalol").setValue("aa"+date.toString());
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) {
                            Log.d(TAG, "Error: ", throwable);
                        }
                    }, new Action() {
                        @Override
                        public void run() {
                            Log.d(TAG, "Completed!");
                        }
                    });

        Date date = TrueTimeRx.now();

我收到此错误:

 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.you.quiz, PID: 14749
java.lang.IllegalStateException: You need to call init() on TrueTime at least once.
    at com.instacart.library.truetime.TrueTime.now(TrueTime.java:29)

我一直在找几天,似乎没有什么可以解决问题,任何建议都值得赞赏。

0 个答案:

没有答案