使用Observable.zip的多个API调用

时间:2019-04-06 12:03:51

标签: android observable retrofit2 rx-java2

我开发了一个RSS阅读器Android应用程序。我想API调用多个,但是不能。我的代码在这里,我在做什么错了?

注意;

  • 响应将是XML,为字符串dasd
  • 请求大小可能会增加(可能是10个)

ArticleServisGenerator;

object ArticleServiceGenerator {

private const val FAKE_URL = "https://api.github.com"

private val interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().apply {
    level = HttpLoggingInterceptor.Level.BASIC
}

private val client: OkHttpClient = OkHttpClient.Builder().apply {
    addInterceptor(interceptor)
}.build()

private val retrofit: Retrofit = Retrofit.Builder()
    .baseUrl(FAKE_URL)
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .client(client)
    .build()

val service: ArticleService = retrofit.create(ArticleService::class.java)}

ArticleService;

interface ArticleService {

@GET
fun getArticlesFromRss(@Url url: String): Observable<ResponseBody>}

和存储库

class Repository {

fun getArticlesFromRss() {
    val request: ArrayList<Observable<*>> = ArrayList()

    // The number may increase here.
    request.add(ArticleServiceGenerator.service.getArticlesFromRss("https://commonsware.com/blog/feed.atom"))
    request.add(ArticleServiceGenerator.service.getArticlesFromRss("https://jfenn.me/blog/feeds/android.xml"))
    request.add(ArticleServiceGenerator.service.getArticlesFromRss("https://arunkumar.dev/feed.xml"))

    Observable.zip(request) { Any() }
        .subscribeOn(Schedulers.io())
        .observeOn(Schedulers.newThread())
        .subscribe({
            print(it.toString())
        }) {
            print(it.toString())
        }
}}

4 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

fun getArticlesFromRss() {
    val request: ArrayList<Observable<ResponseBody>> = ArrayList()
    request.add(ArticleServiceGenerator.service.getArticlesFromRss("https://commonsware.com/blog/feed.atom").subscribeOn(Schedulers.io()))
    request.add(ArticleServiceGenerator.service.getArticlesFromRss("https://jfenn.me/blog/feeds/android.xml").subscribeOn(Schedulers.io()))
    request.add(ArticleServiceGenerator.service.getArticlesFromRss("https://arunkumar.dev/feed.xml").subscribeOn(Schedulers.io()))

    Observable.zip(request){ args -> Arrays.asList(args) }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({
            print(it.toString())
        }) {
            print(it.toString())
        }
}}

答案 1 :(得分:0)

Observable<List<String>> result = Observable.zip(
      observable1.subscribeOn(Schedulers.io()), 
      observable2.subscribeOn(Schedulers.io()), 
      observable3.subscribeOn(Schedulers.io()), 
  new Function3<ResponseType1, ResponseType2, ResponseType3, List<String>>() {
    @Override
    public List<String> apply(ResponseType1 type1, ResponseType2 type2, ResponseType3 type3) {
        List<String> list = new ArrayList();
        list.add(type1.data);
        list.add(type2.data);
        list.add(type3.data);
        return list;
    }
  }
);

答案 2 :(得分:0)

每个可观察的内部zip顺序执行。将subscribeOn(Schedulers.io())添加到每个可观察的getArticles中,它将同时执行它们。

答案 3 :(得分:0)

您只需要指定默认调度程序即可。

.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io))