这是我的翻新方法:
@GET("comments")
Callable<List<Comments>> getCommentsRx();
对于Rxjava东西我已经创建线程类:
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);
}
我有loadjson方法来从网络获取数据:
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;
}
但是我在fetchingServer
方法中出错。
java.lang.IllegalArgumentException:无法为java.util.concurrent.Callable创建调用适配器> 方法JsonplaceholderService.getCommentsRx
答案 0 :(得分:2)
Retrofit没有用于Callable
的适配器,并且您不能在@GET方法中使用它。
您可以使用:
RxJava2 Observable, Flowable, Single, Completable & Maybe
,
Java 8 CompletableFuture
Call
因此,您可以执行以下操作:
@GET("comments")
Observable<List<Comments>> getCommentsRx(); //rx observable, not java.util.observable
在您的客户中:
service.getCommentsRx()
.subscribeOn(scheduler)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(finished, onError)