使用flatMap的Rx Java Retrofit只运行一次

我试图从一个按钮点击流,然后平面地图与另一个可观察的改造,以请求一个端点,但它只能运行一次观察! 当我再次点击时,不要通过平面地图方法。

这是奇怪的原因,当平面地图返回其他observable它运行正常,但与改造之一,它只能运行一次。

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var retrofit:Retrofit = Retrofit.Builder() .baseUrl("<SERVER_IP>") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io())) .build() var testService:TestService = retrofit.create(TestService::class.java) var buttonObservable:Observable<Void> = RxView.clicks(btnRequest) buttonObservable .observeOn(Schedulers.newThread()) .subscribeOn(AndroidSchedulers.mainThread()) .flatMap { Log.i("debug", "run flatmap") var request:Request = Request() request.accessToken = "<acess_token>" testService.test(request) } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ user -> Log.i("debug", "user ") }, { err -> Log.d("debug", "err: ${err.message}") }) } 

测试接口:

 interface TestService { @POST("protected") fun test(@Body request: Request) : Observable<User> } 

请求类:

 class Request { var accessToken:String = "" } 

提前致谢!!!!

编辑:最后,发生了什么事是onError是触发器,停止流。

下面的代码解决了它:

 buttonObservable .observeOn(Schedulers.newThread()) .subscribeOn(AndroidSchedulers.mainThread()) .flatMap { try { Log.i("debug", "run flatmap") var blaService = retrofit.create(TestService::class.java) var request: Request = Request() blaService.test(request) } catch(t:Throwable) { Observable.empty<Void>() } } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ user -> Log.i("debug", "user ") }, { err -> Log.d("debug", "err: ${err.message}") }, { Log.i("debug", "onComplete") }) 

但是这样做不是更好的方法,在onError或onComplete之后有一个正确的方法来继续流?