Search in sources :

Example 1 with OkClient

use of retrofit.client.OkClient in project remusic by aa112901.

the class RestServiceFactory method create.

public static <T> T create(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);
    RequestInterceptor interceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {
            // 7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d", Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };
    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl).setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));
    return builder.build().create(clazz);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RequestInterceptor(retrofit.RequestInterceptor) RestAdapter(retrofit.RestAdapter) Cache(com.squareup.okhttp.Cache)

Example 2 with OkClient

use of retrofit.client.OkClient in project android by testpress.

the class TestpressService method getRestAdapter.

private RestAdapter getRestAdapter() {
    if (authToken != null) {
        OkHttpClient client = new OkHttpClient();
        Interceptor interceptor = new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder header = chain.request().newBuilder();
                header.addHeader("Authorization", getAuthToken());
                return chain.proceed(header.build());
            }
        };
        client.networkInterceptors().add(interceptor);
        restAdapter.setClient(new OkClient(client));
    }
    return restAdapter.build();
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) OkClient(retrofit.client.OkClient) Interceptor(com.squareup.okhttp.Interceptor)

Example 3 with OkClient

use of retrofit.client.OkClient in project Timber by naman14.

the class RestServiceFactory method createStatic.

public static <T> T createStatic(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);
    RequestInterceptor interceptor = new RequestInterceptor() {

        PreferencesUtility prefs = PreferencesUtility.getInstance(context);

        @Override
        public void intercept(RequestFacade request) {
            // 7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,%smax-stale=%d", Integer.valueOf(60 * 60 * 24 * 7), prefs.loadArtistAndAlbumImages() ? "" : "only-if-cached,", Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };
    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl).setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));
    return builder.build().create(clazz);
}
Also used : PreferencesUtility(com.naman14.timber.utils.PreferencesUtility) OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RequestInterceptor(retrofit.RequestInterceptor) RestAdapter(retrofit.RestAdapter) Cache(com.squareup.okhttp.Cache)

Example 4 with OkClient

use of retrofit.client.OkClient in project Rutgers-Course-Tracker by tevjef.

the class ClientModule method providesRMPClient.

@Provides
@Singleton
public RMPClient providesRMPClient(OkHttpClient client, Gson gson) {
    OkHttpClient okClient = client.clone();
    okClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    okClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("http://rutgersapp.tevindev.me:8080/").setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new GsonConverter(gson)).setClient(new OkClient(okClient)).build();
    return new RMPClient(restAdapter.create(ClientService.class));
}
Also used : GsonConverter(retrofit.converter.GsonConverter) RMPClient(com.tevinjeffrey.rmp.client.RMPClient) OkHttpClient(com.squareup.okhttp.OkHttpClient) ClientService(com.tevinjeffrey.rmp.client.ClientService) OkClient(retrofit.client.OkClient) RestAdapter(retrofit.RestAdapter) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 5 with OkClient

use of retrofit.client.OkClient in project Rutgers-Course-Tracker by tevjef.

the class RetroRutgersModule method providesRutgersRestAdapter.

@Provides
@Singleton
public RetroRutgersService providesRutgersRestAdapter(OkHttpClient client, Gson gson) {
    OkHttpClient okClient = client.clone();
    okClient.networkInterceptors().add(getCacheControlInterceptor(TimeUnit.SECONDS.toMillis(5)));
    return new RestAdapter.Builder().setEndpoint("http://sis.rutgers.edu/soc/").setLogLevel(RestAdapter.LogLevel.HEADERS_AND_ARGS).setErrorHandler(new MyErrorHandler()).setClient(new OkClient(okClient)).setConverter(new GsonConverter(gson)).build().create(RetroRutgersService.class);
}
Also used : GsonConverter(retrofit.converter.GsonConverter) OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RestAdapter(retrofit.RestAdapter) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

OkClient (retrofit.client.OkClient)9 OkHttpClient (com.squareup.okhttp.OkHttpClient)7 RestAdapter (retrofit.RestAdapter)7 RequestInterceptor (retrofit.RequestInterceptor)4 GsonConverter (retrofit.converter.GsonConverter)3 Cache (com.squareup.okhttp.Cache)2 Provides (dagger.Provides)2 Singleton (javax.inject.Singleton)2 Gson (com.google.gson.Gson)1 WeatherService (com.linroid.weather.data.WeatherService)1 PreferencesUtility (com.naman14.timber.utils.PreferencesUtility)1 ConnectionPool (com.squareup.okhttp.ConnectionPool)1 Interceptor (com.squareup.okhttp.Interceptor)1 Request (com.squareup.okhttp.Request)1 ClientService (com.tevinjeffrey.rmp.client.ClientService)1 RMPClient (com.tevinjeffrey.rmp.client.RMPClient)1 Bean (org.springframework.context.annotation.Bean)1 Scope (org.springframework.context.annotation.Scope)1 ErrorHandler (retrofit.ErrorHandler)1 Profiler (retrofit.Profiler)1