1
votes

I have a function that takes an article id list to set on the adapter. Everything works fine until at least one of the requests fails. Then the returned list is empty. How to make it ignore a failing request and move on to the next one? For example, I request 5 articles 1 fail, 4 are okay, so I get a list of 4. I know, I need to use onErrorResumeNext() here, but I don't know-how. I am totally new to this, and I would ask for a ready-made code as it would look. Please help :(

Interface:

@GET("articles/{id}")
Observable<Articles> getArticle1(@Path("id") int id);

Activity:

private void getMoreArticles(List<Integer> l) {
    
        ApiInterface apiInterface = ApiClient.getApiClientRX().create(ApiInterface.class);

        List<Observable<?>> requests = new ArrayList<>();
        for (int id : l) {
            requests.add(apiInterface.getArticle1(id));
        }

        Observable.zip(requests, new Function<Object[], List<Articles>>() {
            @Override
            public List<Articles> apply(@NonNull Object[] objects) {
                List<Articles> articlesArrayList = new ArrayList<>();
               
                for (Object response : objects) {
                    articlesArrayList.add((Articles) response);
                }
            
                return articlesArrayList;
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .onErrorResumeNext(Observable.<List<Articles>>empty())
                .subscribe(
                        new Consumer<List<Articles>>() {
                            @Override
                            public void accept(List<Articles> articlesList) {
                                adapter = new Adapter(articlesList, MainActivity.this);
                                if (fav) recyclerView.setAdapter(adapter);
                                else addRV().setAdapter(adapter);
                                adapter.notifyDataSetChanged();
                                initListener();
                                swipeRefreshLayout.setRefreshing(false);
                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable e) throws Exception {

                            }
                        }
                ).isDisposed();
    

}