Mamadou Mamadou - 11 months ago 172
Android Question

RxJava flatmap chaining requests

i am using Retrofit with RxJAva for an app that gets Rss Feeds, but the rss doesn't contain all the informations so i use jsoup to parse every item link, to retrieve the image and the article's description. now i am using it this way:

public Observable<Rss> getDumpData() {
return newsAppService.getDumpData()
.flatMap(rss -> Observable.from(
.flatMap(Checked.f1(item -> Observable.just(Jsoup.connect(
.map(document ->"div[itemprop=image] > img").first())
.doOnNext(element -> item.image = element.attr("src"))

and i am getting an error on this line:

it doesn't recognize rss of the flatmap. and when i move the
in flatmap brackets i have another error saying that the return type must be changed to Element. is their any solution ?

Answer Source

You can't mix internal parameter of one RxJava parameter (flatMap lambda parameter) with another operator parameter (defaultIfEmpty).

First of all, create a helper function to keep main reactive stream cleaner:

private Observable<List<Item>> getDetails(List<Item> items) {
    return Observable.from(items)
               .flatMap(Checked.f1(item ->
                           .map(document ->"div[itemprop=image] > img").first()),
                           (itemInner, element) -> {
                                itemInner.image = element.attr("src");
                                return itemInner;

Then reformat main function:

    .flatMap(rss ->
            (rssInner, items) -> {
       = items;
                return rss;
            }).onErrorResumeNext((throwable -> Observable.just(rss))

Hope I got your aim properly. It may not work, as I am unable to test it, however I hope you get the idea. The reason I used .zip functions it that you can't loose the reference to the currently parsed item or rss