如何在RxJava中处理onError.我收到"OnErrorNotImplementedException"; [英] How to handle onError inside RxJava. I am getting "OnErrorNotImplementedException"

查看:295
本文介绍了如何在RxJava中处理onError.我收到"OnErrorNotImplementedException";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我正在使用ReactiveLocationProvider库(链接).我在onCreate方法中订阅更新.设备在线时工作正常,但是如果我关闭wifi并等待下一个后台位置更新设备,则OnErrorNotImplementedException.这是日志:

In my app I am using ReactiveLocationProvider library (link). I subscribe for updates in the onCreate method. It works fine when the device is online but if I switch my wifi off and wait for the next background location updates the device OnErrorNotImplementedException. This is the log:

12-30 00:05:44.711 12237-12237/koemdzhiev.com.stormy E/AndroidRuntime: FATAL EXCEPTION: main
 Process: koemdzhiev.com.stormy, PID: 12237
 java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:60)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5292)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
  Caused by: rx.exceptions.OnErrorNotImplementedException: Timed out waiting for response from server
     at rx.Observable$27.onError(Observable.java:7535)
     at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:154)
     at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:197)
     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:170)
     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5292) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
  Caused by: java.io.IOException: Timed out waiting for response from server
     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
     at pl.charmas.android.reactivelocation.observables.geocode.ReverseGeocodeObservable.call(ReverseGeocodeObservable.java:34)
     at pl.charmas.android.reactivelocation.observables.geocode.ReverseGeocodeObservable.call(ReverseGeocodeObservable.java:13)
     at rx.Observable.unsafeSubscribe(Observable.java:7710)
     at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
     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)

MainActivity代码:

MainActivity code:

public class MainActivity extends AppCompatActivity {
    ReactiveLocationProvider locationProvider;
Observable<List<Address>> reverseGeocodeObservable;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //-----------MY CODE STARTS HERE-----------------
    //check if the user previously has seen the whats new message...
    sharedPref = getPreferences(Context.MODE_PRIVATE);
    editor = sharedPref.edit();
    if (sharedPref.getInt(getString(R.string.saved_if_whats_new_seen), 1) != 0){
        WhatsNewDialogCreator dialogCreator = new WhatsNewDialogCreator(this, sharedPref);
        dialogCreator.show();
    }

    request = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setSmallestDisplacement(0)
            .setFastestInterval(1 * 60 * 1000)
            .setInterval(60 * 60 * 1000);
    locationProvider = new ReactiveLocationProvider(this);
    //subscribe for background location updates...
    if(isNetworkAvailable()) {
        startBackgroundUpdates();
    }



    if(isFirstTimeLaunchingTheApp) {
        Log.d(TAG, "onCreate getLocation");
        getLocation();
    }
}

private void startBackgroundUpdates() {
    subscription = locationProvider.getUpdatedLocation(request)
            .subscribe(new Action1<Location>() {
                @Override
                public void call(Location location) {
                    Log.d(TAG, "Getting Background updates...");
                    MainActivity.this.latitude = location.getLatitude();
                    MainActivity.this.longitude = location.getLongitude();
                    numOfBackgroundUpdates++;

                    reverseGeocodeObservable = locationProvider
                            .getReverseGeocodeObservable(location.getLatitude(), location.getLongitude(), 1);
                    getLocationName();

                }
            });
}

@Override
protected void onPause() {
    super.onPause();
    /*
    * check if onCreate there was no internet, thus = subscription == null...
    * start the background updates onPause if there is internet
    */
    if (subscription == null && isNetworkAvailable()){
        Log.d(TAG,"startBackgroundUpdates on Pause...");
        startBackgroundUpdates();
    }
}

推荐答案

尝试对位置进行地理编码时发生错误.

There is an error happening when you are trying to geocode the location.

由以下原因引起:java.io.IOException:等待服务器的响应超时在android.location.Geocoder.getFromLocation(Geocoder.java:136)

Caused by: java.io.IOException: Timed out waiting for response from server at android.location.Geocoder.getFromLocation(Geocoder.java:136)

当您使用一个 Action1 作为参数进行订阅时,它仅处理对 onNext 的调用,如果发生错误,则应用程序将崩溃.

When you call subscribe with one Action1 as a parameter it only handles calls to onNext and if an error happens the app will crash.

您需要订阅:

subscribe(new Subscriber<Location>() {
    @Override
    public void onNext(Location location) { /*Handle the location updates*/ }

    @Override
    public void onCompleted() { }

    @Override
    public void onError(Throwable e) { }
})

这篇关于如何在RxJava中处理onError.我收到"OnErrorNotImplementedException";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆