为什么我收到此超时异常?

时间:2016-09-15 07:44:20

标签: android rx-java retrofit2 socket-timeout-exception

我正在构建Android应用程序。并且,有时当我的后端不起作用时,我会收到此错误:

Java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
        at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:114)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:152)
        at android.app.ActivityThread.main(ActivityThread.java:5132)
        at java.lang.reflect.Method.invokeNative(Method.java:-2)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(NativeStart.java:-2)
Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
        at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:192)
        at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
        at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
        at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
        at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:152)
        at android.app.ActivityThread.main(ActivityThread.java:5132)
        at java.lang.reflect.Method.invokeNative(Method.java:-2)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(NativeStart.java:-2)
Caused by: rx.exceptions.CompositeException: 2 exceptions occurred. 
        at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:192)
        at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
        at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
        at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
        at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:152)
        at android.app.ActivityThread.main(ActivityThread.java:5132)
        at java.lang.reflect.Method.invokeNative(Method.java:-2)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(NativeStart.java:-2)
Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
        at android.util.Log.getStackTraceString(Log.java:314)
        at android.util.Slog.e(Slog.java:77)
        at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:125)
        at android.support.v7.app.AppCompatDelegateImplBase$1.uncaughtException(AppCompatDelegateImplBase.java:66)
        at com.bugsnag.android.ExceptionHandler.uncaughtException(ExceptionHandler.java:56)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
        at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
        at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:118)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:152)
        at android.app.ActivityThread.main(ActivityThread.java:5132)
        at java.lang.reflect.Method.invokeNative(Method.java:-2)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(NativeStart.java:-2)
Caused by: java.net.SocketTimeoutException
        at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
        at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
        at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
        at okio.Okio$2.read(Okio.java:138)
        at okio.AsyncTimeout$2.read(AsyncTimeout.java:238)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.java:325)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.java:314)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:210)
        at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
        at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:53)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at com.telnet.asp.data.api.ServiceGenerator$2.intercept(ServiceGenerator.java:124)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at com.telnet.asp.data.api.ServiceGenerator$2.intercept(ServiceGenerator.java:124)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at com.telnet.asp.data.api.ServiceGenerator$2.intercept(ServiceGenerator.java:124)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at com.telnet.asp.data.api.ServiceGenerator$2.intercept(ServiceGenerator.java:124)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at com.telnet.asp.data.api.ServiceGenerator$1.intercept(ServiceGenerator.java:54)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
        at okhttp3.RealCall.execute(RealCall.java:60)
        at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
        at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
        at rx.Subscriber.setProducer(Subscriber.java:211)
        at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
        at rx.internal.operators.OperatorMap$MapSubscriber.setProducer(OperatorMap.java:99)
        at rx.Subscriber.setProducer(Subscriber.java:205)
        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
        at rx.Observable.unsafeSubscribe(Observable.java:8666)
        at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
        at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
        at rx.internal.schedulers.ExecutorScheduler$ExecutorSchedulerWorker.run(ExecutorScheduler.java:104)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException: println needs a message
        at android.util.Log.println_native(Log.java:-2)
        at android.util.Log.d(Log.java:138)
        at com.telnet.asp.presentation.presenter.AccountPresenter$AccountSubscriber.onError(AccountPresenter.java:87)
        at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157)
        at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
        at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:276)
        at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:219)
        at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:152)
        at android.app.ActivityThread.main(ActivityThread.java:5132)
        at java.lang.reflect.Method.invokeNative(Method.java:-2)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
        at dalvik.system.NativeStart.main(NativeStart.java:-2)

当这个错误发生时,这是一段代码(确切位置在返回chain.proceed(request)上):

private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
           .addInterceptor(loggingInterceptor)
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS);

private static Retrofit.Builder builder =
                new Retrofit.Builder()
                        .baseUrl(API_BASE_URL)
                        .addCallAdapterFactory(rxAdapter)
                        .addConverterFactory(GsonConverterFactory.create());

   public static <S> S createService(Class<S> serviceClass, Context context) {
            mContext = context;

            SharedPreferences sharedPreferences = context.getSharedPreferences("Token_pref", Context.MODE_PRIVATE);
            final String authToken = sharedPreferences.getString("token", null);

            if (authToken != null) {
                httpClient.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Interceptor.Chain chain) throws IOException {
                        Request original = chain.request();

                        Request.Builder requestBuilder = original.newBuilder()
                                .header("token", authToken)
                                .method(original.method(), original.body());

                        Request request = requestBuilder.build();
                        return chain.proceed(request);
                    }
                });
            }
            OkHttpClient client = httpClient.build();
            Retrofit retrofit = builder.client(client).build();
            return retrofit.create(serviceClass);
        }

为什么我有这个错误,有人可以帮我吗? 提前谢谢。

0 个答案:

没有答案