使用RxJava链接Retrofit调用

时间:2016-12-07 13:13:24

标签: android retrofit rx-java

我正试图让自己进入RxJava,所以我读了一些关于它的帖子。我想我理解它是如何工作的,但我想提交一个理论代码,以确保我对图书馆的理解。

让我们想象一下,我有一个API可以检索出一个产品列表,并为每个产品列出他们制作的艺术家,他们的专辑和他们的歌曲。 我的模型将是以下

public class Produceur {
    private Integer id;
    private String name;
    private String pictureUrl;
}

public class Artist {
    private Integer id;
    private String name;
    private String lastname;
    private String sceneName;
    private String pictureUrl;
}

public class Album {
    private Integer id;
    private int year;
    private String name;
    private String style;
    private String pictureUrl;
    private Integer artistId;
}

public class Song {
    private Integer id;
    private String title;
    private int duration;
    private String pictureUrl;
    private Integer albumId;
}

使用我的Retrofist服务

@GET("myUrl/produceurs")
Observable<List<ProduceurResponse>> getProduceurs();

@GET("myUrl/produceurs/{produceurId}")
Observable<List<ArtisteResponse>> getArtistForProduceur(@Path("produceurId") Integer produceurId);

@GET("myUrl/picture/{id}")
Observable<ResponseBody> getPicture(@Path("id") Integer id);

响应对象

public class ProduceurResponse {
    private Integer id;
    private String name;
    private String pictureUrl;
}

public class ArtisteResponse {
    private Integer id;
    private String name;
    private String lastname;
    private String sceneName;
    private String pictureUrl;
    private List<AlbumResponse> albums;
}

public class AlbumResponse {
    private Integer id;
    private int year;
    private String name;
    private String style;
    private String pictureUrl;
    private Integer artistId;
    private List<SongResponse> songs;
}

public class SongResponse {
    private Integer id;
    private String title;
    private int duration;
    private String pictureUrl;
    private Integer albumId;
}

我想检索所有产品和每个艺术家,并以相同的顺序保存本地存储器中的所有图像。 我想到了下面的代码,对于每个产品我们将检索艺术家,他们的专辑和歌曲,并将它们插入我们的基地。 一旦我们完成了对产品和艺术家的检索,我们就可以下载图片了(我们将每张图片存储在一个列表中)。

getProduceurs().flatMap(produceurs -> Observable.from(produceurs))
               .doOnNext(produceur -> insertProduceurInBase(produceur))
               .subscribe(produceur -> Observable.from(getArtistForProduceur(produceur.getId())
                                                 .flatMap(artists -> Observable.from(artists))
                                                 .doOnNext(artist -> insertArtistInBase(artist)))
                                                 .subscribe(),

                          e -> showError(e),

                          () -> Observable.from(listOfIdsToDownload)
                                          .doOnNext(id -> getPicture(id))
                                          .subscribe(response -> createImage(response),

                                          e -> showError(e),

                                          () -> isFinished()
                                          )
                         );

这段代码是否有效(我想是的,但我不确定)?这是最好的方法吗?

现在,如果我的getProduceurs服务返回一个包含产品艺术家ID的ProduceurResponse列表,我得到了一个服务来检索艺术家profil,另一个用于检索其专辑。

public class ProduceurResponse {
    private Integer id;
    private String name;
    private String pictureUrl;
    List<Integer> artistsIds;
}

public class ArtisteProfileResponse {
    private Integer id;
    private String name;
    private String lastname;
    private String sceneName;
    private String pictureUrl;
}

@GET("myUrl/artist/{artistId}")
Observable<List<ArtisteProfileResponse>> getArtistProfile(@Path("artistId") Integer artistId);

@GET("myUrl/artist/{artistId}/detail")
Observable<List<AlbumResponse>> getArtistAlbums(@Path("artistId") Integer artistId);

我可以使用.zip使getArtistProfile()和getArtistAlbums()同时调用

getProduceurs().flatMap(produceurs -> Observable.from(produceurs))
               .doOnNext(produceur -> insertProduceurInBase(produceur))
               .subscribe(produceur -> Observable.from(produceur.getArtistIds())
                                                 .zip(
                                                        getArtistProfile(),
                                                        getArtistAlbums(),
                                                        (artistProfil, albumList) -> insertArtistInBase()
                                                 )
                                                 .subscribe(),

                          e -> showError(e),

                          () -> Observable.from(listOfIdsToDownload)
                                          .doOnNext(id -> getPicture(id))
                                          .subscribe(response -> createImage(response),

                                          e -> showError(e),

                                          () -> isFinished()
                                          )
                         );

但我真的不确定我是否正确使用拉链。拉链是否在这段代码中使用得很好?这会有用吗?这是最好的方法吗?

编辑

所以我尝试使用google books api实现类似于我原来想法的东西。

我有一个Retrofit界面

public interface IBookService {
    @GET("volumes?q=robot+subject:fiction")
    Observable<BookSearchResult> getFictionAuthors(@Query("category") String key);
    @GET("volumes")
    Observable<BookSearchResult> getBooksForAuthor(@Query("q") String author, @Query("category") String key);
}

public class BookSearchResult {
    public List<BookResult>  items;
}

public class BookResult {
    public String id;
    public String selfLink;
    public VolumeInfoResult volumeInfo;
    public SaleInfoResult saleInfo;
}

我尝试用字符串机器人(getFictionAuthors)检索小说书籍,并返回一个包含BookResult列表的BookSearchResult。对于每本书的结果,我用getBooksForAuthor检索作者的所有书籍。我的代码在

之下
Observable<BookResult> observable = mWebService.getFictionAuthors(API_KEY)
                .flatMap(new Func1<BookSearchResult, Observable<BookResult>>() {

                    // Parse the result and build a CurrentWeather object.
                    @Override
                    public Observable<BookResult> call(final BookSearchResult data) {
                        return Observable.from(data.items);
                    }
                })
                .concatMap(new Func1<BookResult, Observable<BookSearchResult>>() {

                    // Parse the result and build a CurrentWeather object.
                    @Override
                    public Observable<BookSearchResult> call(final BookResult data) {
                        return mWebService.getBooksForAuthor("=inauthor:" + data.volumeInfo.authors.get(0), API_KEY);
                    }
                })
                .flatMapIterable(new Func1<BookSearchResult, List<BookResult>>() {

                    // Parse the result and build a CurrentWeather object.
                    @Override
                    public List<BookResult> call(final BookSearchResult data) {
                        return data.items;
                    }
                })
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BookResult>() {
                    @Override
                    public void onNext(final BookResult book) {
                        Log.e("Book","Book is " + book.volumeInfo.title + " written by " + book.volumeInfo.authors.get(0));
                    }

                    @Override
                    public void onCompleted() {
                        Log.e("Book","Book list completed");
                    }

                    @Override
                    public void onError(final Throwable error) {
                        Log.e("Book","Book list error");
                    }
                }

这段代码正在运行但有些奇怪我不明白。在我的日志中,我首先从第一个作者的getBooksForAuthor请求返回,然后从该作者的每本书中返回日志。在那之后,我得到了第二作者的请求结果和他书中的部分日志。按照其他作者的要求结果,然后是第二作者的书籍清单的结尾和所有其他作者的书籍清单。

为了说明这一点,我的日志看起来像

- > Return from request for Author 1
- > Book 1 from author 1
...
- > Book 10 from author 1
- > Return from request for Author 2
- > Book 1 from author 2
...
- > Book 5 from author 2
- > Return from request for Author 3
- > Return from request for Author 4
...
- > Return from request for Author 10
- > Book 6 from author 2
..
- > Book 10 from author 2
- > Book 1 from author 3
..
- > Book 10 from author 3
...
- > Book 1 from author 10
..
- > Book 10 from author 10

当我预料到

- > Return from request for Author 1
- > Book 1 from author 1
...
- > Book 10 from author 1
- > Return from request for Author 2
- > Book 1 from author 2
...
- > Book 10 from author 2
...
- > Return from request for Author 10
- > Book 1 from author 10
...
- > Book 10 from author 10

有人有解释或理解我缺少的东西吗?

1 个答案:

答案 0 :(得分:2)

您应该避免嵌套订阅(请查看flatmap运算符)。它是一个代码气味的指标。 要避免嵌套订阅,您可以使用运算符flatMap(不是garanti结果事件顺序)或concatMap(garanti结果事件顺序)。

我也注意到你在完成的回调中使用了另一个Observable:在这种情况下你可以concat可观察到。

所以使用这种运算符重新编写代码:

 getProduceurs().flatMap(produceurs -> Observable.from(produceurs))
                .doOnNext(produceur -> insertProduceurInBase(produceur))
                // call getArtistForProduceur and emit results in order
                .concatMap(produceur -> getArtistForProduceur(produceur.getId()))
                // emits items of the list
                .flatMapIterable(artists -> artists)
                .doOnNext(artist -> insertArtistInBase(artist)))
                // don't care about elements. But will wait for the completion of the previous observable
                .ignoreElements()
                // perform jobs after the previous observable complete
                .concatWith(Observable.from(listOfIdsToDownload)
                                      .doOnNext(id -> getPicture(id))
                                      .doOnNext(response -> createImage(response)))
                // show an error if an error occur in the downstream
                .doOnError(e -> showError(e))
                // call isFinished when everything is finished.
                .doOnCompleted(() -> isFinished())
                .subscribe()