Okhttp: I use rxjava2+retrofit2+okhttp3 in my android application and get crashed when app not connected to internet

Created on 8 Oct 2017  路  5Comments  路  Source: square/okhttp

Here is my sendHttpRequest mothed in BaseActivity:

  public <T> Disposable sendHttpRequest(Observable<Response<T>> observable, final GolfRequestCallback<T> requestCallback) {
        Disposable httpSubscribe = observable.compose(TransformerHelper.<Response<T>>io_main()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Disposable disposable) throws Exception {
                requestCallback.onBefore();
            }
        }).doOnError(new Consumer<Throwable>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                requestCallback.onAfter();
                requestCallback.onError(throwable);
            }
        }).doOnComplete(new Action() {
            @Override
            public void run() throws Exception {
                requestCallback.onAfter();
            }
        }).subscribe(new Consumer<Response<T>>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Response<T> response) throws Exception {
                try {
                    requestCallback.onAfter();
                    if (response.isSuccess()) {
                        requestCallback.onSuccess(response.data, response.msg);
                    } else {
                        if ("mid_error".equals(response.code)) {
                            AccountUtils.INSTANCE.loginOut();
                            LoginActivity.startActivity(BaseActivity.this);
                        }
                        requestCallback.onBizErr(response.code, response.msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                throwable.printStackTrace();
            }
        });
        return httpSubscribe;
    }

Log

io.reactivex.exceptions.OnErrorNotImplementedException: failed to connect to /192.168.20.122 (port 8888) after 10000ms
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:74)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:119)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.observers.DisposableLambdaObserver.onError(DisposableLambdaObserver.java:64)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.checkTerminated(ObservableObserveOn.java:276)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:172)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.os.Looper.loop(Looper.java:135)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5314)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
09-29 10:54:18.151 16007-16007/com.tiemagolf W/System.err: Caused by: java.net.SocketTimeoutException: failed to connect to /192.168.20.122 (port 8888) after 10000ms
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at java.net.Socket.connect(Socket.java:882)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:69)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:238)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.RealConnection.connectTunnel(RealConnection.java:210)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:152)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at com.tiemagolf.api.GolfHeaderInterceptor.intercept(GolfHeaderInterceptor.java:44)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at okhttp3.RealCall.execute(RealCall.java:77)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
09-29 10:54:18.152 16007-16007/com.tiemagolf W/System.err:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.Observable.subscribe(Observable.java:10903)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-29 10:54:18.153 16007-16007/com.tiemagolf W/System.err:     at java.lang.Thread.run(Thread.java:818)

Most helpful comment

Network errors are a certainty when writing mobile apps, and if you're consuming them with RxJava then you need to provide an error handler. See: https://github.com/ReactiveX/RxJava/wiki/Error-Handling.

All 5 comments

1) you didn't implement exceptions for RxJava
2) Socket Timeout

Network errors are a certainty when writing mobile apps, and if you're consuming them with RxJava then you need to provide an error handler. See: https://github.com/ReactiveX/RxJava/wiki/Error-Handling.

Thanks, I have solved this problem.

I have the same problem.How to solve

I have the same problem.How to solve

Was this page helpful?
0 / 5 - 0 ratings