Return 可从 android 上的改造方法调用
Return Callable from retrofit method on android
这是我的 Retrofit 方法:
@GET("comments")
Callable<List<Comments>> getCommentsRx();
我已经为 Rxjava 创建了线程 class :
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: Unable to create call adapter for java.util.concurrent.Callable>
for method JsonplaceholderService.getCommentsRx
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)
这是我的 Retrofit 方法:
@GET("comments")
Callable<List<Comments>> getCommentsRx();
我已经为 Rxjava 创建了线程 class :
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: Unable to create call adapter for java.util.concurrent.Callable> for method JsonplaceholderService.getCommentsRx
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)