Reputation: 657
I am new in RXjava. I have impliment it in my project but it is not getting the data and didnot display it. what is the problem here?
My viewModel
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;
}
ViewModel 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;
}
.......................................................................................................................................................................................
Upvotes: 0
Views: 199
Reputation: 52
Try this,
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
Upvotes: 0
Reputation: 1300
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);
Upvotes: 1