Change ConnectionTimeout to re-equip OkHttpClient at runtime

I am using Dagger to add RestAdapter.Builder, and I am doing OkHttpClient inside this function. The function is as follows:

@Provides @Singleton @Named("JSON") public RestAdapter.Builder provideJsonRestAdapterBuilder(final ChangeableEndpoint changeableEndpoint, final Set<RequestInterceptor> requestInterceptors) { final OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS); okHttpClient.setWriteTimeout(Constants.WRITE_TIMEOUT, TimeUnit.SECONDS); okHttpClient.setReadTimeout(Constants.READ_TIMEOUT, TimeUnit.SECONDS); try { okHttpClient.setCache(new Cache(mApplication.getCacheDir(), Constants.CACHE_SIZE)); } catch (final IOException e) { LOG.warn(e.getMessage(), e); } final Gson gson = new GsonBuilder() .registerTypeAdapter(Date.class, new DateDeserializer()) .registerTypeAdapter(AbstractAction.class, new AbstractActionDeserializer()) .create(); return new RestAdapter.Builder() .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : retrofit.RestAdapter.LogLevel.NONE) .setConverter(new GsonConverter(gson)) .setEndpoint(changeableEndpoint) .setClient(new OkClient(okHttpClient)) .setRequestInterceptor(new RequestInterceptor() { @Override public void intercept(final RequestFacade request) { for (final RequestInterceptor requestInterceptor : requestInterceptors) { requestInterceptor.intercept(request); } } }); } 

If you see that I am currently providing a constant when setting setConnectTimeout. But this value will now come from the server, so I need to install it again. Any idea how this can be done?

+6
source share

Source: https://habr.com/ru/post/978094/


All Articles