gpt4 book ai didi

java - 改造:由以下原因引起:java.lang.IllegalArgumentException:无法找到 CustomClass 的调用适配器

转载 作者:行者123 更新时间:2023-12-01 09:20:09 31 4
gpt4 key购买 nike

我创建了 Call<T> 的自定义实现,这里是没有自定义代码的自定义类,只是前向代码给你看。

public class CachedCall<T> implements Call<T> {

private final Call<T> delegate;

public CachedCall(Call<T> delegate) {
this.delegate = delegate;
}

@Override
public Response<T> execute() throws IOException {
return delegate.execute();
}

@Override
public void enqueue(Callback<T> callback) {
delegate.enqueue(callback);
}

public void enqueueWithCache(final CachedCallback<T> callback) {
delegate.enqueue(callback);
}

@Override
public boolean isExecuted() {
return delegate.isExecuted();
}

@Override
public void cancel() {
delegate.cancel();
}

@Override
public boolean isCanceled() {
return delegate.isCanceled();
}

@Override
public Call<T> clone() {
return new CachedCall<>(delegate.clone());
}

@Override
public Request request() {
return delegate.request();
}

}

然后在我的 ApiService 中,我在某些调用中使用了此自定义实现,在其他调用中使用了默认实现,例如:

public interface APIService {

@GET("categories")
Call<List<Categorie>> categories(@Query("tag") String tag);

@GET("categories/{categorie}/quotes")
CachedCall<List<Gif>> gifs(@Path("categorie") String categorie);

当调用自定义方法时,我发生了崩溃:

 Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for CustomClass.
Tried:
* retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
* retrofit2.ExecutorCallAdapterFactory
at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:237)
at retrofit2.Retrofit.callAdapter(Retrofit.java:201)
at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:232)
... 21 more

我需要在某处使用 Retrofit 注册我的自定义实现吗?

最佳答案

我已经解决了我自己的问题。

您需要创建并注册自己的CallAdapter.Factory:

public class CachedCallAdapterFactory extends CallAdapter.Factory {

final Executor callbackExecutor;

public CachedCallAdapterFactory(Executor callbackExecutor) {
this.callbackExecutor = callbackExecutor;
}

@Override
public CallAdapter<Call<?>> get(final Type returnType, final Annotation[] annotations, final Retrofit retrofit) {
if (getRawType(returnType) != CachedCall.class) {
return null;
}
final Type responseType = getParameterUpperBound(0, (ParameterizedType) returnType);
return new CallAdapter<Call<?>>() {
@Override public Type responseType() {
return responseType;
}

@Override public <R> Call<R> adapt(Call<R> call) {
return new CachedCall<>(callbackExecutor, call, responseType, retrofit, annotations);
}
};
}

}

然后在创建 Retrofit 实例时注册它:

        retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(URL)
.addCallAdapterFactory(new CachedCallAdapterFactory(new DefaultExecutor()))
.build();

您的DefaultExecutor只需运行其Runnable

    private class DefaultExecutor implements Executor {

@Override
public void execute(@NonNull Runnable runnable) {
runnable.run();
}
}

关于java - 改造:由以下原因引起:java.lang.IllegalArgumentException:无法找到 CustomClass 的调用适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40216070/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com