如何使用 Observables、RxJava 和 Android 访问 Webapi?
How to use Observables, RxJava and Android to access a Webapi?
我正在尝试使用 RxJava 链接请求列表,但我不知道如何正确执行。
我有一个列表 (LIST_A),我想遍历它并将其数据发送到第一个服务 (SERVICE_A)。一旦收到响应,我必须将新列表 (LIST_B) 发送到第二个 Web 服务 (SERVICE_B)。
流动:
-> 循环 (LIST_A)
发送 ITEM_A ->>
发送此 ITEM_A 之后(它需要启动第二个循环 (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,但它比我预期的要难。
嗯,你需要更好地描述你的问题;使用一些伪代码或序列图。
一种方法大致如下:
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
调用,只需将接口方法设为 return Observables。
我正在尝试使用 RxJava 链接请求列表,但我不知道如何正确执行。 我有一个列表 (LIST_A),我想遍历它并将其数据发送到第一个服务 (SERVICE_A)。一旦收到响应,我必须将新列表 (LIST_B) 发送到第二个 Web 服务 (SERVICE_B)。 流动: -> 循环 (LIST_A) 发送 ITEM_A ->> 发送此 ITEM_A 之后(它需要启动第二个循环 (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,但它比我预期的要难。
嗯,你需要更好地描述你的问题;使用一些伪代码或序列图。
一种方法大致如下:
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
调用,只需将接口方法设为 return Observables。