在运行时更改改造OkHttpClient的ConnectionTimeout

时间:2014-11-12 15:44:50

标签: android retrofit dagger robospice

我正在使用Dagger注入RestAdapter.Builder,我在该函数中创建了一个OkHttpClient。功能如下:

@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);
                }
            }
        });
}

如果您在设置setConnectTimeout时看到我正在提供常量。但是这个值现在将来自服务器,所以我需要再次设置它。知道如何做到这一点吗?

0 个答案:

没有答案