2016-10-24 74 views
0

我已經創建了一個自定義實現Call<T>,這裏是沒有自定義代碼的自定義類,只是轉發代碼供您查看。翻新:導致:java.lang.IllegalArgumentException:無法找到CustomClass的調用適配器

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 

是否需要註冊我的自定義實現w ith在某處改造?

+0

你使用RxJava2嗎? – Raghunandan

+0

Nop,我不知道。我看到了一些答案,添加「.addCallAdapterFactory(RxJavaCallAdapterFactory.create())」來解決這個問題,但它沒有奏效。 – Dimillian

+0

你可以發佈你的翻新服務電話嗎? – Raghunandan

回答

1

我已經解決了我自己的問題。

您需要創建並註冊自己的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 = 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(); 
    } 
} 
+0

另外還有一個樣例,它在翻新github存儲庫上類似https://github.com/square/retrofit/blob/master/samples/src/main/java/com/example/retrofit/ErrorHandlingAdapter.java。 – Raghunandan

相關問題