按顺序发出具有不同请求正文的多个 API 请求

用例:我需要使用 android 客户端(改造)在服务器中发送一些请求。在我得到第一个答案后,我需要更新发送对象值(取决于我得到的最后一个项目)并重新发送它,直到所有数据都下载完毕。我想知道如何使用 Retrofit 和 RxJava 实现这一点(我不想使用 while 循环等)

编辑:问题是,我不知道“平面地图”的确切数量,因为数据可能会变大或变小。我有 420000 条记录,对于每个请求,我都下载 1000 条数据


LEATH
浏览 168回答 2
2回答

12345678_0001

您可以使用flatMap它们,并在下一个中使用它的响应,通过使用it参数,这是前一个的响应。mathApi.multiplyByTwo(1)&nbsp; &nbsp; .flatMap {&nbsp; &nbsp; &nbsp; &nbsp; mathApi.multiplyByTwo(it)&nbsp; &nbsp; }.flatMap {&nbsp; &nbsp; &nbsp; &nbsp; mathApi.multiplyByTwo(it)&nbsp; &nbsp; }.subscribe {&nbsp; &nbsp; &nbsp; &nbsp; // here "it" will be 4 (1*2*2)&nbsp;&nbsp; &nbsp; }如果您不知道flatMap最终会有多少个s,例如,您可以使用递归函数来完成。private fun multiplyByTwo(number: Int) {&nbsp; &nbsp; mathApi.multiplyByTwo(number).subscribe {&nbsp; &nbsp; &nbsp; &nbsp; if (it < Integer.MAX_VALUE) { // When you run out of data.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; multiplyByTwo(it)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

牛魔王的故事

您可以使用保存可变状态的 generate 函数:data class ApiResponse(&nbsp; &nbsp; val nextPage: Int? = null)data class GeneratorState(&nbsp; &nbsp; var lastResponse: ApiResponse)fun makeApiCall(page: Int): ApiResponse {&nbsp; &nbsp; return ApiResponse(page + 1)}Flowable&nbsp; &nbsp; .generate(&nbsp; &nbsp; &nbsp; &nbsp; Callable { GeneratorState(makeApiCall(0)) },&nbsp; &nbsp; &nbsp; &nbsp; BiConsumer { state: GeneratorState, emitter: Emitter<ApiResponse> ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val latest = state.lastResponse&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (latest.nextPage != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val response = makeApiCall(latest.nextPage)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state.lastResponse = response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; emitter.onNext(response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; emitter.onComplete()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; .subscribe(object : FlowableSubscriber<ApiResponse> {&nbsp; &nbsp; &nbsp; &nbsp; var subscription: Subscription? = null&nbsp; &nbsp; &nbsp; &nbsp; override fun onSubscribe(s: Subscription) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subscription = s&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.request(1)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; override fun onNext(response: ApiResponse) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println("onNext :$response")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.nextPage != null && response.nextPage < 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subscription?.request(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subscription?.cancel()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; override fun onComplete() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println("Completed")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; override fun onError(t: Throwable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t.printStackTrace()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java