New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set httpExecutor in Retrofit 2? #1259
Comments
Retrofit 2 uses OkHttp's async API (assuming you use |
Thanks for quick reply |
Our of curiosity, are you just trying to monitor the background threads or On Thu, Nov 5, 2015, 3:03 PM Damian Marusic notifications@github.com
|
I needed to supply my own Executor because when I run tests using Robolectric I want calls to be executed synchronously, on main thread. Manage to do it now by setting up Dispatcher, which is created by my implementation of ExecutorService, as you suggested. |
Ah, OK. Great! On Thu, Nov 5, 2015, 4:01 PM Damian Marusic notifications@github.com
|
@dmarusic35 Can you provide a snippet of how to go about creating the dispatcher and setting it up on the rest client? |
@pkhivesara I provide @Provides
@Singleton
public Dispatcher provideDispatcher() {
return new Dispatcher();
} On the other hand, for tests, I use @Provides
@Singleton
public Dispatcher provideDispatcher() {
// this dispatcher will ensure networking is done on the main thread
return new Dispatcher(new SynchronousExecutorService());
}
private class SynchronousExecutorService implements ExecutorService {
@Override
public void shutdown() {
}
@NonNull
@Override
public List<Runnable> shutdownNow() {
return null;
}
@Override
public boolean isShutdown() {
return false;
}
@Override
public boolean isTerminated() {
return false;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return false;
}
@NonNull
@Override
public <T> Future<T> submit(Callable<T> task) {
return null;
}
@NonNull
@Override
public <T> Future<T> submit(Runnable task, T result) {
return null;
}
@NonNull
@Override
public Future<?> submit(Runnable task) {
return null;
}
@NonNull
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return null;
}
@NonNull
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
return null;
}
@NonNull
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return null;
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException {
return null;
}
@Override
public void execute(Runnable command) {
command.run();
}
} And I just set provided dispatcher to OkHttpClient: OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.dispatcher(dispatcher);
... |
Retrofit 1 has
RestAdapter.Builder().setExecutors
method which sets both httpExecutor and callbackExecutor while Retrofit 2 allows only to set callbackExecutor. Is there any way to set httpExecutor in Retrofit 2?The text was updated successfully, but these errors were encountered: