tldr; 如何将API网络响应Single<ResponseReportObject>
与Flowable<Double>
发射器结合使用,以显示上传进度以及来自UI中API的网络响应。
我是RxJava的新手,目前,我已按照此示例显示我的Retrofit API调用的上传进度:https://medium.com/@PaulinaSadowska/display-progress-of-multipart-request-with-retrofit-and-rxjava-23a4a779e6ba所以现在,我正在尝试执行示例中的第一个侧注在上面的链接,但我不知道如何实现这一目标。
这是我在关注博文后所拥有的:
fun sendReportWithAttachments(remoteReportObject: RemoteReportObject): Flowable<Double> {
return Flowable.create({
try {
val fileParts = prepareFileParts(remoteReportObject.attachments, it)
val reportPart = createPartFromReport(remoteReportObject)
val response = reportEndpoint.postReportWithAttachments(reportPart, fileParts).blockingGet()
it.onComplete()
} catch (e: Exception) {
it.tryOnError(e)
}
}, BackpressureStrategy.LATEST)
}
它会正确地将上传进程发送到UI,但它不会对API的响应做任何处理。所以为了结合我已经尝试过的结果:
fun sendReportWithAttachments(remoteReportObject: RemoteReportObject): Flowable<ResponseBodyWithProgress> {
var response: Single<ResponseReportObject>? = null
val progression: Flowable<Double> = Flowable.create({
try {
val fileParts = prepareFileParts(remoteReportObject.attachments, it)
val reportPart = createPartFromReport(remoteReportObject)
// network call and the parsed response
response = reportEndpoint.postReportWithAttachments(reportPart, fileParts)
it.onComplete()
} catch (e: Exception) {
it.tryOnError(e)
}
}, BackpressureStrategy.LATEST)
// todo wrap the response with the progression in a wrapper Flowable object called ResponseBodyWithProgress
return Flowable.zip(response?.toFlowable(), progression,
BiFunction<ResponseReportObject?, Double, ResponseBodyWithProgress> {
responseReportObject: ResponseReportObject, progress: Double ->
ResponseBodyWithProgress(responseReportObject, progress)
})
}
但不幸的是,这不起作用。我相信我应该使用.zip
运算符,但我不确定如何在这种情况下应用它。我希望有人能引导我朝着正确的方向前进。