Search in sources :

Example 1 with RequestInterceptor

use of retrofit.RequestInterceptor 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 RequestInterceptor

use of retrofit.RequestInterceptor in project connect-android-sdk by telenordigital.

the class RestHelper method buildApi.

private static RestAdapter buildApi(String endpoint) {
    OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).build();
    final Gson gson = new GsonBuilder().registerTypeAdapter(IdToken.class, new IdTokenDeserializer()).create();
    final RequestInterceptor connectRetroFitInterceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", "application/json");
        }
    };
    return new RestAdapter.Builder().setClient(new Ok3Client(httpClient)).setEndpoint(endpoint).setRequestInterceptor(connectRetroFitInterceptor).setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE).setConverter(new GsonConverter(gson)).build();
}
Also used : GsonConverter(retrofit.converter.GsonConverter) IdToken(com.telenor.connect.id.IdToken) OkHttpClient(okhttp3.OkHttpClient) Ok3Client(com.jakewharton.retrofit.Ok3Client) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) IdTokenDeserializer(com.telenor.connect.id.IdTokenDeserializer) Gson(com.google.gson.Gson) RequestInterceptor(retrofit.RequestInterceptor)

Example 3 with RequestInterceptor

use of retrofit.RequestInterceptor 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 RequestInterceptor

use of retrofit.RequestInterceptor in project steps by linroid.

the class WeatherApp method initWeatherService.

private void initWeatherService() {
    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(Config.ENDPOINT).setClient(new OkClient(new OkHttpClient())).setLogLevel(RestAdapter.LogLevel.BASIC).setErrorHandler(new ErrorHandler() {

        @Override
        public Throwable handleError(RetrofitError retrofitError) {
            Timber.e(retrofitError, "访问 %s 出错", retrofitError.getUrl());
            return retrofitError;
        }
    }).setRequestInterceptor(new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade requestFacade) {
            requestFacade.addQueryParam("ak", Config.BAIDU_AK);
            requestFacade.addEncodedQueryParam("mcode", Config.M_CODE);
            requestFacade.addQueryParam("output", "json");
        }
    }).build();
    weatherService = restAdapter.create(WeatherService.class);
}
Also used : ErrorHandler(retrofit.ErrorHandler) OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) WeatherService(com.linroid.weather.data.WeatherService) RequestInterceptor(retrofit.RequestInterceptor) RestAdapter(retrofit.RestAdapter) RetrofitError(retrofit.RetrofitError)

Example 5 with RequestInterceptor

use of retrofit.RequestInterceptor in project divide by HiddenStage.

the class AbstractWebManager method createRestAdapter.

private RestAdapter createRestAdapter(Config config) {
    RestAdapter.Builder builder = new RestAdapter.Builder();
    builder.setClient(new OkClient(config.client)).setEndpoint(config.serverUrl).setLogLevel(RestAdapter.LogLevel.FULL).setLog(new RestAdapter.Log() {

        @Override
        public void log(String s) {
            retrologger.debug(s);
        }
    }).setConverter(new GsonConverter(new Gson())).setRequestInterceptor(new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade requestFacade) {
            onRequest(requestFacade);
        }
    }).setProfiler(new Profiler() {

        @Override
        public Object beforeCall() {
            return null;
        }

        @Override
        public void afterCall(RequestInformation requestInformation, long l, int i, Object o) {
            retrologger.error("afterCall(" + requestInformation.getRelativePath() + ":" + requestInformation.getMethod() + ": " + i + " : " + o);
            requestEventPublisher.onNext(new RequestObject(requestInformation, l, i, o));
        }
    });
    return builder.build();
}
Also used : GsonConverter(retrofit.converter.GsonConverter) OkClient(retrofit.client.OkClient) Gson(com.google.gson.Gson) RequestInterceptor(retrofit.RequestInterceptor) RequestFacade(retrofit.RequestInterceptor.RequestFacade) Profiler(retrofit.Profiler) RestAdapter(retrofit.RestAdapter)

Aggregations

RequestInterceptor (retrofit.RequestInterceptor)5 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 OkHttpClient (com.squareup.okhttp.OkHttpClient)3 Gson (com.google.gson.Gson)2 Cache (com.squareup.okhttp.Cache)2 GsonConverter (retrofit.converter.GsonConverter)2 GsonBuilder (com.google.gson.GsonBuilder)1 Ok3Client (com.jakewharton.retrofit.Ok3Client)1 WeatherService (com.linroid.weather.data.WeatherService)1 PreferencesUtility (com.naman14.timber.utils.PreferencesUtility)1 IdToken (com.telenor.connect.id.IdToken)1 IdTokenDeserializer (com.telenor.connect.id.IdTokenDeserializer)1 OkHttpClient (okhttp3.OkHttpClient)1 ErrorHandler (retrofit.ErrorHandler)1 Profiler (retrofit.Profiler)1 RequestFacade (retrofit.RequestInterceptor.RequestFacade)1 RetrofitError (retrofit.RetrofitError)1