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);
}
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();
}
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);
}
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);
}
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();
}
Aggregations