Mamadou
Mamadou

Reputation: 76

RxJava android 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() {
    System.out.println("in the getDumpData");
    if (newsAppService == null) {
        System.out.println("newsAppServer is null");
    }
    return newsAppService.getDumpData()
            .flatMap(new Func1<Rss, Observable<Rss>>() {
                @Override
                public Observable<Rss> call(Rss rss) {
                    List<Item> items = new ArrayList<Item>();
                    for (int i = 0; i < rss.channel.items.size(); i++) {
                        Document document = null;
                        try {
                            document = Jsoup.connect(rss.channel.items.get(i).link).get();
                            Element element = document.select("div[itemprop=image] > img").first();
                            Element bodyElement = document.select("div[class=articlebody]").first();
                            System.out.println("got element " +bodyElement.toString());
                            rss.channel.items.get(i).image = element.attr("src");
                            items.add(rss.channel.items.get(i));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                    rss.channel.items = items;
                    rss.version = "Mams :D";
                    return Observable.just(rss);
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

is it clean ? is there a better way ?

Upvotes: 1

Views: 455

Answers (1)

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

Let's see...

public Observable<Rss> getDumpData() {
  System.out.println("in the getDumpData");
  if (newsAppService == null) {
    System.out.println("newsAppServer is null");
  }
  return newsAppService.getDumpData()
     .flatMap(rss -> Observable
       .from(rss.channel.items)
       .observeOn(Schedulers.io())
       .flatMap(Checked.f1(item -> Observable
         .just(Jsoup.connect(item.link).get())
         .map(document -> document.select("div[itemprop=image] > img").first())
         .doOnNext(element -> item.image=element.attr("src"))
        ))
      )
      .ignoreElements()
      .defaultIfEmpty(rss)
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread());
}

The class checked is from rxJava-extras, but you can write one on your own easily. And yes, Java 8 really helps with Rx; the above will make all calls to the network simultaneously.

Upvotes: 1

Related Questions