use of okhttp3.Interceptor in project Varis-Android by dkhmelenko.
the class TravisRestClient method getHttpClient.
private OkHttpClient getHttpClient() {
final String userAgent = String.format("TravisClient/%1$s", PackageUtils.getAppVersion());
return mOkHttpClient.newBuilder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder request = original.newBuilder().header("User-Agent", userAgent).header("Accept", "application/vnd.travis-ci.2+json");
String accessToken = mAppSettings.getAccessToken();
if (!TextUtils.isEmpty(accessToken)) {
String headerValue = String.format("token %1$s", accessToken);
request.addHeader("Authorization", headerValue);
}
return chain.proceed(request.build());
}
}).build();
}
use of okhttp3.Interceptor in project mobile-sdk-android by meniga.
the class MockClient method getApi.
public static MenigaAPI getApi(Interceptor myInterceptor) {
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(myInterceptor).build();
final Retrofit retrofit = new Retrofit.Builder().addConverterFactory(new MenigaTransactionsConverter()).addConverterFactory(new MenigaFeedConverter(new MockFeedItemFactory())).addConverterFactory(new MenigaCategoryConverter()).addConverterFactory(new MenigaOfferConverter()).addConverterFactory(new MenigaRedemptionConverter()).addConverterFactory(new MenigaReimbursementAccountConverter()).addConverterFactory(new MenigaIdNameKeyValConverter()).addConverterFactory(new MenigaChallengesConverter(new ChallengeItemFactory())).addConverterFactory(new MenigaFeedItemConverter(new MockFeedItemFactory())).addConverterFactory(new MenigaBaseConverter()).addConverterFactory(GsonConverterFactory.create()).baseUrl("http://meniga.is/api/").client(client).build();
return retrofit.create(MenigaAPI.class);
}
use of okhttp3.Interceptor in project SeeWeather by xcc3641.
the class RetrofitSingleton method initOkHttp.
private static void initOkHttp() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// 缓存 http://www.jianshu.com/p/93153b34310e
File cacheFile = new File(C.NET_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = chain -> {
Request request = chain.request();
if (!Util.isNetworkConnected(BaseApplication.getAppContext())) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
Response.Builder newBuilder = response.newBuilder();
if (Util.isNetworkConnected(BaseApplication.getAppContext())) {
int maxAge = 0;
// 有网络时 设置缓存超时时间0个小时
newBuilder.header("Cache-Control", "public, max-age=" + maxAge);
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
newBuilder.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale);
}
return newBuilder.build();
};
builder.cache(cache).addInterceptor(cacheInterceptor);
if (BuildConfig.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
// 设置超时
builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
// 错误重连
builder.retryOnConnectionFailure(true);
sOkHttpClient = builder.build();
}
use of okhttp3.Interceptor in project run-wallet-android by runplay.
the class RunIotaAPICore method postConstruct.
/**
* added header for IRI
*/
private void postConstruct() {
boolean USE_AUTH = false;
// if(host.contains(".runplay.com") || host.contains(".runpg.com"))
// USE_AUTH=true;
final String nodeUrl = protocol + "://" + host + ":" + port;
if (USE_AUTH) {
// Log.e("IRI-CONNECT","Using Auth OK");
String creds = Base64.encodeToString((uname + ":" + upassword).getBytes(), false);
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
OkHttpClient client = builder.readTimeout(5000, TimeUnit.SECONDS).addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest;
newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).addHeader("Authorization", "Basic " + creds).build();
return chain.proceed(newRequest);
}
}).connectTimeout(5000, TimeUnit.SECONDS).build();
// use client to create Retrofit service
final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
service = retrofit.create(IotaAPIService.class);
} else {
// Log.e("IRI-CONNECT","NOOTTTTT Using Auth");
final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request newRequest;
newRequest = request.newBuilder().addHeader(X_IOTA_API_VERSION_HEADER_NAME, X_IOTA_API_VERSION_HEADER_VALUE).build();
return chain.proceed(newRequest);
}
}).connectTimeout(5000, TimeUnit.SECONDS).build();
// use client to create Retrofit service
final Retrofit retrofit = new Retrofit.Builder().baseUrl(nodeUrl).addConverterFactory(GsonConverterFactory.create()).client(client).build();
service = retrofit.create(IotaAPIService.class);
}
// Create OkHttpBuilder
log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
use of okhttp3.Interceptor in project chefly_android by chef-ly.
the class LoggingInterceptor method intercept.
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
long t1 = System.nanoTime();
Log.d("Interceptor", (String.format("Sending request %s on %s%n%s", request.url(), chain.connection(), request.headers())));
Response response = chain.proceed(request);
long t2 = System.nanoTime();
Log.d("Interceptor", (String.format(Locale.US, "Received response for %s in %.1fms%n%s", response.request().url(), (t2 - t1) / 1e6d, response.headers())));
return response;
}
Aggregations