我正在处理多个POST请求,我希望等待服务器在每次POST后返回响应。
这是演示者的代码:
public void sendMessage(final Message message, final String mediaFileName) {
messageSender.sendMessage(message)
.subscribeOn(processScheduler)
.subscribe(new DisposableSubscriber<Response<ResponseBody>>() {
@Override
public void onNext(Response<ResponseBody> responseBodyResponse) {
if (responseBodyResponse.code() >= 200 && responseBodyResponse.code() < 300 && responseBodyResponse.isSuccessful()) {
SharedPrefs.setLastMessageId(message.getMsgId());
Timber.d("Sent message: " + message.getContent() + " with id " + message.getMsgId());
}
}
@Override
public void onError(Throwable t) {
Timber.e("Error occurred while attempting to send message: " + t.getMessage());
}
@Override
public void onComplete() {
if (mediaFileName != null) {
retrieveMediaFile(mediaFileName, message);
}
}
});
}
这是互动器:
public class MessageSenderImpl implements MessageSender {
private final Service service;
private final AuthCredentials authCredentials;
public MessageSenderImpl(Service service, AuthCredentials authCredentials) {
this.service = service;
this.authCredentials = authCredentials;
}
@Override
public Flowable<Response<ResponseBody>> sendMessage(Message message) {
return service.sendMessage(authCredentials.auth(), message);
}
}
以及我的请求:
@POST("someEndPoint")
Flowable<Response<ResponseBody>> sendMessage(@Header("Authorization") String auth, @Body Message message);
例如,如果我发送了10个POST,那么第3个请求的响应可能会在第10个请求之后返回。
注:RX世界的新手。提前谢谢!
也许,您可以使用Observable.zip(Observable1,Observable2,…(
请看这里