如何使用retrofit和rxjava调用n次休息服务并在结尾解释结果?

问题描述 投票:1回答:1

我试图在调用n次休息服务后处理结果,但是一个属性告诉我是否要调用Web服务的对象列表,但是在完成n次调用时我无法处理最终结果,它似乎这是由于异步,有人可以告诉我我做错了什么吗?非常感谢,并原谅我糟糕的英语。

示例代码是:

private List<ResquestService> request;

private void process(){
    Observable.fromIterable(request)
    .filter(resquestService -> resquestService.getType().equals(WS_TYPE))
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.newThread())
    .subscribe(
            resquestService -> {consumeRestService(resquestService);},
            Throwable::printStackTrace,
            () -> {processFinalResults();}
    );
}

private void consumeRestService(ResquestService resquestService){
    getApi().processData(resquestService)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.newThread())
            .subscribe(
                    next -> {processResultWS();},
                    error -> {handleErrorWS();}
            );
}

public class ResquestService {
    private int id;
    private String name;
    private String type;
}
android retrofit2
1个回答
0
投票

解决方案是放入可迭代的.subscribeOn(Schedulers.newThread())

并在对休息服务的调用中:.observeOn(AndroidSchedulers.mainThread())

private void process(){
    Observable.fromIterable(request)
    .filter(resquestService -> resquestService.getType().equals(WS_TYPE))
    .subscribeOn(Schedulers.newThread())
    .subscribe(
            resquestService -> {consumeRestService(resquestService);},
            Throwable::printStackTrace,
            () -> {processFinalResults();}
    );
}

private void consumeRestService(ResquestService resquestService){
    getApi().processData(resquestService)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    next -> {processResultWS();},
                    error -> {handleErrorWS();}
            );
}
© www.soinside.com 2019 - 2024. All rights reserved.