I'm new to RxJava, using RxJava 3 to return a response that contains a dynamic number of urls that I then need to make GET requests to and then add those responses to a list that is to be observed by the ui. Since the number of urls returned is unknown, I'm unsure how to go about this. I tried populating a list with the urls and then looping through them - not the right way to go about it & did not work:
public void getSection() {
disposables.add(repository.getContent()
.subscribeOn(Schedulers.io())
.map(new Function<Content, List<Section>>() {
@Override
public List<Section> apply(Content content) throws Throwable {
return content.getContent().getSections().stream().filter(live -> live.getSectionType().equals("shows")).collect(Collectors.toList());
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<List<Section>>() {
@Override
public void onNext(@NonNull List<Section> sections) {
contentSourcesURIs = new ArrayList<>();
for (int i = 0; i < sections.get(0).getContent().get(0).getContent().size(); i++) {
contentSourcesURIs.add(sections.get(0).getContent().get(0).getContent().get(i).getSource().getUri());
}
sectionList.postValue(sections);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "getSection: " + e.getMessage());
}
@Override
public void onComplete() {
System.out.println(Thread.currentThread());
for(int i = 0; i < contentSourcesURIs.size(); i++){
getVideoContent(Collections.singletonList(contentSourcesURIs.get(i)));
}
}
}));
}
public void getVideoContent(List<String> contentSourcesURIs) {
for (int i = 0; i < contentSourcesURIs.size(); i++) {
getVideosContent(contentSourcesURIs.get(i));
}
}
public void getVideosContent(String contentSourcesURI) {
String authHeader = URL.POLICY_KEY;
disposables.add(repository.getVideosPlaylistContent(authHeader, contentSourcesURI)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Video>() {
@Override
public void onNext(@NonNull Video video) {
videosList.add(video);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "getVideosContent: " + e.getMessage());
}
@Override
public void onComplete() {
videoPlaylist.postValue(videosList);
}
}));
}
Is it possible to use RxJava to achieve what I'm trying to do? Thanks in advance!