如何使用 rxJava 进行乘法请求

How to make multiply request with rxJava

如何通过改造发出 10 个并行请求。请帮我解决这个问题。我尝试使用 zip 运算符,但我只能在 Java 中使用 Function9.

public Single<List<Response<MyResponse>>> getRequest(...) {

        return Single.zip(getNyRequest(sessionId, RequestParams.getParams( "")),
                getNyRequest(sessionId, RequestParams.getParams( "")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                getNyRequest(sessionId, RequestParams.getParams("")),
                (result1, result2, result3, result4, result5, result6, result7, result8, result9, result10) -> {

                });
}

你可以使用迭代:

  List<Single<String>> singles = Arrays.asList(Single.just("1"), Single.just("2"), Single.just("3"));
  Single<List<String>> zip = Single.zip(singles, objects -> Stream.of(objects).map(o -> (String) o).collect(Collectors.toList()));

你的情况:

 List<Single<Response<MyResponse>>> singles = Arrays.asList(getNyRequest(sessionId, RequestParams
        .getParams("")), getNyRequest(sessionId, RequestParams.getParams("")), ....);
 Single<List<Response<MyResponse>>> zip = Single.zip(singles,
        objects -> Stream.of(objects).map(o -> (Response<MyResponse>) o).collect(Collectors.toList()));

已编辑: 每 10 分钟重复一次:

getRequest(...).repeatWhen(c -> c.delay(10, TimeUnit.MINUTES))
.subscribe();