javaandroidkotlin

RxJava not displaying the data


I am new in RXjava. I have implemented it in my project but it is not getting the data and did not display it. What is the problem here?

My view model:

public LiveData<Resource<List<Item>>> makeApiCallTopArticles() {

    final MutableLiveData<Resource<List<Item>>> mediumObjectsList = new MutableLiveData<>();
    mediumObjectsList.setValue(Resource.loading());
    APIService apiService = RetroInstant.getRetroMediumClient().create(APIService.class);
    Observable<CnnResponse> observable = apiService.getNewsObjectsList("http://rss.cnn.com/rss/cnn_topstories.rss",
            "", "25");

    Observer<CnnResponse> observer = new Observer<CnnResponse>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(CnnResponse value) {
            List<Item> articles = new ArrayList<>();
            assert value != null;
            List<Item> responce = value.getItems();
            for (int i = 0; i < Objects.requireNonNull(responce).size(); i ++) {
                if (!Objects.equals(Objects.requireNonNull(responce.get(i).getEnclosure()).getLink(), null) && !Objects.equals(responce.get(i).getTitle(), "")) {
                    articles.add(responce.get(i));
                }
            }
            mediumObjectsList.postValue(Resource.success(articles));
        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {

        }
    };
    observable.subscribe(observer);
    return mediumObjectsList;
}

View model before I added RXjava:

public LiveData<Resource<List<Item>>> makeApiCallTopArticles() {
    final MutableLiveData<Resource<List<Item>>> mediumObjectsList = new MutableLiveData<>();
    mediumObjectsList.setValue(Resource.loading());
    APIService apiService = RetroInstant.getRetroMediumClient().create(APIService.class);
    Call<CnnResponse> call = apiService.getNewsObjectsList("http://rss.cnn.com/rss/cnn_topstories.rss",
            "", "25");
    call.enqueue(new Callback<CnnResponse>() {
        @Override
        public void onResponse(@NotNull Call<CnnResponse> call, @NotNull Response<CnnResponse> response) {
            List<Item> articles = new ArrayList<>();
            assert response.body() != null;
            List<Item> responce = response.body().getItems();
            for (int i = 0; i < Objects.requireNonNull(responce).size(); i ++) {
                if (!Objects.equals(Objects.requireNonNull(responce.get(i).getEnclosure()).getLink(), null) && !Objects.equals(responce.get(i).getTitle(), "")) {
                    articles.add(responce.get(i));
                }
            }
            mediumObjectsList.postValue(Resource.success(articles));

        }

        @Override
        public void onFailure(@NotNull Call<CnnResponse> call, @NotNull Throwable t) {
            mediumObjectsList.setValue(Resource.error(t.getMessage() != null ? t.getMessage() : "Unknown Error"));
        }
    });

    return mediumObjectsList;
}

Solution

  • Try to add logs to: onNext and onError method. Just to understand that you really receive a response or maybe you have some errors during the request. If you receive an error that can be a reason.

    When you're using Rx you should use schedulers to avoid perform long term operation on the main thread. replace you subscription with:

    observable.observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(observer);