0

如何使用 RxSwift 和 alamofire 获得嵌套 api 调用的响应?在这里,我得到了来自 alamofire api cal 的响应,因此我需要调用另一个 api 调用。我想获得第二个 api 调用响应。谁能建议我解决这个问题的解决方案。请。

func origin() -> Observable<String> {
return Alamofire.request("httpbin.org/get").rx.responseJSON() 

}

func otherApiCall(with origin: String) -> Observable<YourType> {
// Other api call using origin
return Alamofire...........

}

然后

origin()
.flatMap{ origin in 
    otherApiCall(with: origin)  
}
.subscribe(onNext: { response in 

})
.disposed(by: disposeBag)
4

1 回答 1

0

您的代码解释不多,但根据我的理解,如果您想使用其他 api 的响应调用 api,而不是下面的代码应该可以工作。

func firstRequest() -> Observable<firstRequestResponseType> {
    return firstRequest
}

firstRequest
.flatMap { (firstRequestResponseType) ->  Observable<secondRequestResponseType>
    return secondRequest
}
.map { (secondRequestResponseType)
 //you can user second request’s response here
}
.subscribe()
于 2018-02-27T08:34:22.203 回答