我正在使用observable模式从服务器处理我的responseBody。
目标:这是我正在使用的代码,在错误的情况下我想要显示带有错误消息的toast,只是为了测试现在我已经放了一个日志消息并且它完美地显示了日志消息。但是,当我尝试在该方法中执行任何其他操作时,即显示带有错误消息的Toast或通过总线将消息推送到活动时崩溃并显示错误
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
Caused by: rx.exceptions.OnErrorNotImplementedException: Can't create handler inside thread that has not called Looper.prepare()
我做错了什么?
myRetrofit.startPatrol(path.getId(), 8)
.map(result -> {
if (result.isSuccessful()) {
try {
String responseBody = result.body().string();
JSONObject fullObject = new JSONObject(responseBody);
String patrolID = fullObject.getString("id");
pref.setPatrolID(patrolID);
emitStoreChange(new CobaltStoreChangeEvent(Constants.PATROL_STARTED));
Log.d(TAG, "startPatrol: " + patrolID);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
} else {
Utils.errorHandle(activity, result);
}
//I dont need to return anything here I just want success.
return null;
})
.onErrorReturn(throwable -> {
//Errors are on this toast line.
Toasty.error(App.getAppContext(), throwable.getMessage()).show();
//tried calling emitStoreChange method as above but same result in error. Putting only log works.
Log.d(TAG, "startPatrol: "+throwable.getMessage());
return throwable.getMessage();
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe();