链接Retrofit调用RxJava并返回主对象

时间:2016-02-10 19:23:34

标签: android retrofit rx-java

我正在尝试使用retrofit + rxjava来进行嵌套的api调用。基本上我必须构建一个Library对象列表。每个Library对象都有一个Authors列表,每个Author对象都有一个Books列表。这就是我的json的结构。

图书馆json -

{
 title: "Library",
 items: [
      {
        title: "Library1"
        image: "http://example.com/image/101/image_lib1.png"
        url: "http://example.com/api/1"
      },
      {
        title:"Library2"
        image: "http://example.com/image/101/image_lib2.png"
        url: "http://example.com/api/2"
      },
      {
        title:"Library3"
        image: "http://example.com/image/101/image_lib3.png"
        url: "http://example.com/api/3"
      }
   ]
 }

和作者json-

{
 title: "Authors",
 items: [
      {
        title: "J.K Rowling"
        image: "http://example.com/image/101/image_author1.png"
        url: "http://example.com/api/101"
      },
      {
        title:"Mark Twain"
        image: "http://example.com/image/101/image_author2.png"
        url: "http://example.com/api/201"
      },
      {
        title:"Charles Dickens"
        image: "http://example.com/image/101/image_author3.png"
        url: "http://example.com/api/301"
      }
   ]
 }

和书籍json-

{
description: Books,
imageurl: "http://example.com/image/101/image_101.png",
items: [
   {
     id: 101,
     title: "Oliver Twist ",
     description: "some description"
   },
  {
   id: 1011,
   title: "Hard times",
   description: "some more description."
  }
}
]
}

我的api界面如下。

  @GET("/api/")
  Observable<LibraryResponse> getLibraryList();

  @GET("/api/{feedId}")
  Observable<AuthorResponse> getAuthorList(@Path("feedId") String feedId);

  @GET("/api/{feedId}")
  Observable<BooksResponsee> getBooksList(@Path("feedId") String feedId);




Observable<List<Library>> observable = myRestInterface
    .getLibraryList()
    .map(a -> a.getItems())
    .flatMapIterable(b -> b)                 
    .flatMap(h -> myRestInterface                                                                 
                  .getAuthorList(h.getUrl().substring(h.getUrl().lastIndexOf("/") + 1))                                       
                  .map(x -> x.getItems())
                  .flatMapIterable(l -> l)
                  .flatMap(e -> myRestInterface
                                .getBooksList(e.getUrl().substring(e.getUrl().lastIndexOf("/") + 1))
                                .map(d -> d.getItems())
                                .flatMapIterable(c -> c)))

    .collect(// ? into list of main object)
    .doOnNext(mainObjectList -> new ViewupdateMethod(mainObjectList));
    .subscribe(...);

我正在尝试构建一个可用于更新UI的Librarylist对象。我有点被困在这里。进行多次调用后,如何保存结果并将所有内容收集到库对象列表中?

1 个答案:

答案 0 :(得分:3)

    Observable<List<Library>> observable = myRestInterface
            .getLibraryList()
            .flatMapIterable(response -> response.getItems())
            .doOnNext(library -> myRestInterface
                    .getAuthorList(library.getUrl().substring(library.getUrl().lastIndexOf("/") + 1))
                    .flatMapIterable(response -> response.getItems())
                    .doOnNext(author -> library.getAuthors().add(author))
                    .doOnNext(
                            author -> myRestInterface
                                    .getBooksList(author.getUrl().substring(author.getUrl().lastIndexOf("/") + 1))
                                    .flatMapIterable(response -> response.getItems())
                                    .doOnNext(book -> author.getBooks().add(book))
                                    .subscribe()
                    )
                    .subscribe()
            )

            .doOnNext(mainObjectList -> new ViewupdateMethod(mainObjectList))
            .subscribe();

注意.subscribe()次来电。它们用于开始执行整个链。在订户到达之前,RxObservable不执行任何操作。