This is my method on Retrofit:
@GET("comments")
Callable<List<Comments>> getCommentsRx();
I have created Thread class for Rxjava stuff :
public static <T> Disposable async(Callable<List<T>> task, Consumer<List<T>> finished, Consumer<Throwable> onError) {
return async(task, finished, onError, Schedulers.io());
}
public static <T> Disposable async(Callable<List<T>> task, Consumer<List<T>> finished,
Consumer<Throwable> onError, Scheduler scheduler) {
finished = finished != null ? finished
: (a) -> {
};
onError = onError != null ? onError
: throwable -> {
};
return Single.fromCallable(task)
.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(finished, onError);
}
I have loadjson method to fetch data from network:
private void loadJson(Consumer<List<Comments>> finished) {
Threading.async(() -> fetchingServer(),finished,null);
}
private List<Comments> fetchingServer() {
JsonplaceholderService service =
ServiceGenerator.createService(JsonplaceholderService.class);
try {
return service.getCommentsRx().call();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
but i got error in fetchingServer
method.
java.lang.IllegalArgumentException: Unable to create call adapter for java.util.concurrent.Callable> for method JsonplaceholderService.getCommentsRx
Retrofit doesn't have adapters for Callable
and you can't use it in your @GET method.
You can use:
RxJava2 Observable, Flowable, Single, Completable & Maybe
,
Java 8 CompletableFuture
Call
So, you can do something like this:
@GET("comments")
Observable<List<Comments>> getCommentsRx(); //rx observable, not java.util.observable
In your client:
service.getCommentsRx()
.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(finished, onError)