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

Sam*_*mys 5 android rx-java retrofit

我正在尝试使用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"
      }
   ]
 }
Run Code Online (Sandbox Code Playgroud)

和作者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"
      }
   ]
 }
Run Code Online (Sandbox Code Playgroud)

和书籍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."
  }
}
]
}
Run Code Online (Sandbox Code Playgroud)

我的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(...);
Run Code Online (Sandbox Code Playgroud)

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

Kir*_*kov 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();
Run Code Online (Sandbox Code Playgroud)

注意.subscribe()通话。它们用于启动整个链的执行。在订阅者到达之前,RxObservable 不执行任何操作。