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

标签 java android networking retrofit

我创建了 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/

相关文章:

java - 无法在 Liferay 6.2 社区版中部署 Liferay Hook

android - 如何创建 Rx 搜索 View 来使用查询过滤回收器 View

c - 我的获取 IP 地址 C 函数未输出正确的值

java - 如何从方法级别范围检索值?

java - 带有 List<String> 的原始类型给出编译错误

java - JAVA中的多线程

java - JAX-RS : Authenticate with multiple principals (more than just username)

android - 将选项卡布局中的图标左对齐

android - 至于TextView,以编程方式改变样式?

c++ - 在 Qt 中获取文件的网络路径