如何将对象从 `flatMap` 传递到链中的 `subscribe`?
How to pass an object from `flatMap` to `subscribe` in a chain?
如何在链中将对象从 flatMap
传递到 subscribe
?在下面的代码片段中,如何沿着链访问 blog
对象?
Single<HttpResponse<JsonNode>> singleHttpClient = httClient.post();
singleHttpClient .toObservable().flatMap(httpResponse -> {
if(httpResponse.getStatus() == 200) {
JsonNode responseNode = httpResponse.getBody();
List<JSONObject> blogObjects = iterateFrom(responseNode.getObject().getJSONArray("blogs"))
}
return Observable.fromIterable(blogObjects).toList().toObservable();
}).flatMap(jsonObjects -> Observable.fromIterable(jsonObjects))
.flatMap(jsonObject -> {
return Observable.just(new Blog(jsonObject.getString("id"), jsonObject.getString("guid")));
}).flatMap(blog -> {
Single<HttpResponse<JsonNode>> singleHttpClient2 = httpClient2.post();
singleHttpClient2.getParams().put("guid", blog.getImageGuid());
return singleHttpClient2.postAsBinary().toObservable();
}).subscribe(javaScriptObjectHttpResponse -> {
JavaScriptObject jsoBody = javaScriptObjectHttpResponse.getBody();
doSomethingWith(blog, jsBody); // How to access `blog` from here?
});
在 project-reactor 中,存在一个名为 zip
的运算符用于绑定 Monos 和 Fluxes(类似于 rx-java 中的 Observables),以便可以将多个项目向下传递(作为元组)管道。
rx-java 应该存在类似的运算符。
有用的堆栈溢出问题,
使用 zipWith
方法,例如 Blog
对象中的 below.Create 变量,保存来自服务器的博客数据响应,并为该数据创建 getter 和 setter变量。
Single<HttpResponse<JsonNode>> singleHttpClient = httClient.post();
singleHttpClient .toObservable().flatMap(httpResponse -> {
if(httpResponse.getStatus() == 200) {
JsonNode responseNode = httpResponse.getBody();
List<JSONObject> blogObjects = iterateFrom(responseNode.getObject().getJSONArray("blogs"))
}
return Observable.fromIterable(blogObjects).toList().toObservable();
}).flatMap(jsonObjects -> Observable.fromIterable(jsonObjects))
.flatMap(jsonObject -> {
return Observable.just(new Blog(jsonObject.getString("id"), jsonObject.getString("guid")));
}).flatMap(blog -> {
Single<HttpResponse<JsonNode>> singleHttpClient2 = httpClient2.post();
singleHttpClient2.getParams().put("guid", blog.getImageGuid());
return Observable.zipWith(singleHttpClient2.postAsBinary().toObservable(), (Blog blog, HttpResponse data)->blog.setData(data));
}).subscribe(blog -> {
doSomethingWith(blog); // How to access `blog` from here?
});
如何在链中将对象从 flatMap
传递到 subscribe
?在下面的代码片段中,如何沿着链访问 blog
对象?
Single<HttpResponse<JsonNode>> singleHttpClient = httClient.post();
singleHttpClient .toObservable().flatMap(httpResponse -> {
if(httpResponse.getStatus() == 200) {
JsonNode responseNode = httpResponse.getBody();
List<JSONObject> blogObjects = iterateFrom(responseNode.getObject().getJSONArray("blogs"))
}
return Observable.fromIterable(blogObjects).toList().toObservable();
}).flatMap(jsonObjects -> Observable.fromIterable(jsonObjects))
.flatMap(jsonObject -> {
return Observable.just(new Blog(jsonObject.getString("id"), jsonObject.getString("guid")));
}).flatMap(blog -> {
Single<HttpResponse<JsonNode>> singleHttpClient2 = httpClient2.post();
singleHttpClient2.getParams().put("guid", blog.getImageGuid());
return singleHttpClient2.postAsBinary().toObservable();
}).subscribe(javaScriptObjectHttpResponse -> {
JavaScriptObject jsoBody = javaScriptObjectHttpResponse.getBody();
doSomethingWith(blog, jsBody); // How to access `blog` from here?
});
在 project-reactor 中,存在一个名为 zip
的运算符用于绑定 Monos 和 Fluxes(类似于 rx-java 中的 Observables),以便可以将多个项目向下传递(作为元组)管道。
rx-java 应该存在类似的运算符。
有用的堆栈溢出问题,
使用 zipWith
方法,例如 Blog
对象中的 below.Create 变量,保存来自服务器的博客数据响应,并为该数据创建 getter 和 setter变量。
Single<HttpResponse<JsonNode>> singleHttpClient = httClient.post();
singleHttpClient .toObservable().flatMap(httpResponse -> {
if(httpResponse.getStatus() == 200) {
JsonNode responseNode = httpResponse.getBody();
List<JSONObject> blogObjects = iterateFrom(responseNode.getObject().getJSONArray("blogs"))
}
return Observable.fromIterable(blogObjects).toList().toObservable();
}).flatMap(jsonObjects -> Observable.fromIterable(jsonObjects))
.flatMap(jsonObject -> {
return Observable.just(new Blog(jsonObject.getString("id"), jsonObject.getString("guid")));
}).flatMap(blog -> {
Single<HttpResponse<JsonNode>> singleHttpClient2 = httpClient2.post();
singleHttpClient2.getParams().put("guid", blog.getImageGuid());
return Observable.zipWith(singleHttpClient2.postAsBinary().toObservable(), (Blog blog, HttpResponse data)->blog.setData(data));
}).subscribe(blog -> {
doSomethingWith(blog); // How to access `blog` from here?
});