【问题标题】:How to use Observables, RxJava and Android to access a Webapi?如何使用 Observables、RxJava 和 Android 访问 Webapi?
【发布时间】:2016-06-09 00:13:36
【问题描述】:

我正在尝试使用 RxJava 来链接请求列表,但我不知道如何正确执行此操作。 我有一个列表(LIST_A),我想遍历它并将其数据发送到第一个服务(SERVICE_A)。一旦我得到响应,我必须将一个新列表 (LIST_B) 发送到第二个 Web 服务 (SERVICE_B)。 流动: -> 循环(LIST_A) 发送 ITEM_A ->> AFTER SENDING THIS ITEM_A(它需要启动第二个 LOOP (LIST_B) 并发送所有项目) -- 结束循环

这就是我到目前为止所做的:

private static <T> Observable<T> makeObservable(final Callable<T> func) {
    return Observable.create(
            new Observable.OnSubscribe<T>() {
                @Override
                public void call(Subscriber<? super T> subscriber) {
                    try {
                        subscriber.onNext(func.call());
                    } catch(Exception ex) {
                        Log.e("OP_DAO", "Error reading from the database", ex);
                    }
                }
            });
}

public Observable<List<VisitInfo>> getVisitObservable() {
    return makeObservable(this.getVisit())
            .subscribeOn(Schedulers.computation()) ;
}

public Callable<List<VisitInfo>> getVisit() {
    return new Callable<List<PesquisaInfo>>() {
        @Override
        public List<VisitInfo> call() throws Exception {
            List<VisitInfo> list = new ArrayList<VisitInfo>();

            // 1. build the query
            String query = "SELECT * FROM "
                    + VISIT ;
            // 2. get reference to writable DB

            try {
                Cursor cursor = getMyWritableDatabase().rawQuery(query, null);
                // 3. go over each row, build book and add it to list
                if (cursor.moveToFirst()) {
                    do {
                        VisitInfo v = new VisitInfo();
                        ...
                        list.add(v);
                    } while (cursor.moveToNext());
                    closeCursor(cursor);
                }
                return list;
            } finally {
                closeDatabase();
            }
        }
    };

}

Client client = new ServiceGenerator().createService(Client.class);
Call<List<WS01>> wsServiceCall = client.GetData(user);
Call<List<WS02>> wsService2Call = client.GetData2(dependent of first call);

任何人都可以帮助我,我花了几天时间试图了解如何使用 RxJava 和 Retrofit,但它比我预期的要难。

【问题讨论】:

  • 所以,不清楚你想做什么;您需要以下哪一项? for(A in list_a){clientA.sendA}; for(B in list_B){clientB.sendB};for(A in list_a){clientA.sendA; for(B in list_B){clientB.sendB}; }; ?
  • 第二个选项,但是 sendB 需要在 sendA 收到应答后执行。感谢您尝试帮助 Tassos Bassoukos,我非常感谢!

标签: android asp.net asp.net-web-api rx-java retrofit2


【解决方案1】:

嗯,你需要更好地描述你的问题;使用一点伪代码或序列图。

一种做你想做的事情的方法大致如下:

import static Observable.*;

public List<VisitInfo> getVisit() { /* just do the db things here */ }

return defer(() -> from(getVisit()))
      .flatMap(visit ->
          fromCallable(client.getData(visit))
          .flatMapIterable(otherList -> otherList)
          .flatMap(other -> fromCallable(client.getData2(visit, other))));

如果你使用 Retrofit,那么你甚至不需要 fromCallable 调用,只需让接口方法返回 Observables。

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多