Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用RxJava和Android的多个API调用_Android_Api_Rx Java - Fatal编程技术网

使用RxJava和Android的多个API调用

使用RxJava和Android的多个API调用,android,api,rx-java,Android,Api,Rx Java,我有一个对象列表,这些对象应该按顺序传递给API,并且在所有调用完成后需要最终回调。代码结构如下所示 //Singleton class public class RestAPIManager { public Completable doSomething(A a) { //Make API call here and return Completeable } } //Class from which the above API call is made

我有一个对象列表,这些对象应该按顺序传递给API,并且在所有调用完成后需要最终回调。代码结构如下所示

//Singleton class
public class RestAPIManager {

    public Completable doSomething(A a) {
        //Make API call here and return Completeable
    }
}

//Class from which the above API call is made
public class FragmentA extends Fragment {
    List<A> aList = new ArrayList<A>;

    //This method should return completable after all calls are made
    private Completable callDoSomething(List<A> data) {
        // The code that I tried
        Observable.just(data)
            .flatMap(new <List<A>, Observable<?>>() {
                @Override
                public Observable<?> call(List<A> dataList) {
                    //WHAT SHOULD BE RETURNED FROM HERE
                    for (A a : dataList) {
                        RestAPIManager.getInstance().doSomething(a)    
                    }
                }
            })
            .doOnCompleted(new Action0() {
                @Override
                public void call() {

                }
            })
            .doOnError(new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {

                }
            })
            .toCompletable(); 
    }
}
//单例类
公共类RestAPIManager{
公共完全剂量测定法(A){
//在此处进行API调用并返回Completable
}
}
//从中进行上述API调用的类
公共类FragmentA扩展了Fragment{
列表列表=新的数组列表;
//所有调用完成后,此方法应返回completable
私有可完成的callDoSomething(列表数据){
//我试过的代码
可观察。仅(数据)
.flatMap(新调用(列表数据列表){
//从这里应该归还什么
对于(A:数据列表){
RestAPIManager.getInstance().doSomething(a)
}
}
})
.doOnCompleted(新操作0(){
@凌驾
公开作废通知(){
}
})
.doon错误(新操作1(){
@凌驾
公共无效呼叫(可丢弃可丢弃){
}
})
.toCompletable();
}
}

我刚刚开始使用RxJava,所以我真的不知道如何继续使用它

您可以使用
from
操作符展平列表,然后
concatMap
按顺序处理每个项目:

Observable.from(data)
    .concatMap(new Func1<A, Observable<?>>() {
        @Override
        public Observable<?> call(A a) {
            return RestAPIManager.getInstance().doSomething(a);
        }
    })
    .toCompletable();
Observable.from(数据)
.concatMap(新Func1调用(A){
返回RestAPIManager.getInstance().doSomething(a);
}
})
.toCompletable();

对于android,请使用以下内容

public void doIt(List<String> data) {
    final Subscription subscription = proceed(data)
            .subscribe(new Subscriber<List<String>>() {
                @Override
                public void onCompleted() {
                    //final callback here
                    System.out.println("on completed");
                }

                @Override
                public void onError(Throwable e) {
                    //invokes on error
                    System.out.println("on error");
                }

                @Override
                public void onNext(List<String> strings) {
                    //invokes on items emits
                    strings.forEach(System.out::print);
                }
            });
}

private Observable<List<String>> proceed(List<String> data) {
    return Observable.from(data)
            .map(RestApiManager.getInstance()::doSomething)
            .toList();
}
public void doIt(列表数据){
最终订阅=继续(数据)
.subscribe(新订户(){
@凌驾
未完成的公共无效(){
//这里是最后的回拨
系统输出打印项次(“完成时”);
}
@凌驾
公共无效申报人(可丢弃的e){
//错误时调用
System.out.println(“错误时”);
}
@凌驾
public void onNext(列出字符串){
//在项目发出时调用
strings.forEach(System.out::print);
}
});
}
私有可观测数据(列表数据){
从(数据)中可观察到的返回
.map(RestApiManager.getInstance()::doSomething)
.toList();
}
并通过订阅取消订阅请求执行。unsubscribe();当onStop()调用以避免泄漏时。不要忘记调度程序