Having recently tried to learn RxJava, I wanted to replace my async tasks in Android in my viewmodel class with RxJava2. However when I run it, I get a "NetworkOnMainThreadException" exception when running the following:
private void requestLocationDetails() {
try {
compositeDisposable.add(Observable
.just(JsonRestCaller.readJsonFromUrl(buildUrl()))
.subscribeOn(Schedulers.io())
.subscribeWith(new DisposableObserver<JsonObject>() {
@Override
public void onNext(JsonObject jsonObject) {
try {
parseJson(jsonObject);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
@Override
public void onError(Throwable e) {
Log.e(TAG, e.getMessage());
}
@Override
public void onComplete() {
}
}));
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
As far as I have understood,
.subscribeOn(Schedulers.io())
causes my method to run on a background thread but Android states otherwise. Am I missing something here?