Search in sources :

Example 36 with Interceptor

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();
}
Also used : Request(okhttp3.Request) Interceptor(okhttp3.Interceptor)

Example 37 with Interceptor

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);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MenigaFeedItemConverter(com.meniga.sdk.converters.MenigaFeedItemConverter) MenigaIdNameKeyValConverter(com.meniga.sdk.converters.MenigaIdNameKeyValConverter) MenigaFeedConverter(com.meniga.sdk.converters.MenigaFeedConverter) MenigaTransactionsConverter(com.meniga.sdk.converters.MenigaTransactionsConverter) MenigaReimbursementAccountConverter(com.meniga.sdk.converters.MenigaReimbursementAccountConverter) ChallengeItemFactory(com.meniga.sdk.helpers.ChallengeItemFactory) MockFeedItemFactory(com.meniga.sdk.helpers.MockFeedItemFactory) Retrofit(retrofit2.Retrofit) MenigaChallengesConverter(com.meniga.sdk.converters.MenigaChallengesConverter) MenigaBaseConverter(com.meniga.sdk.converters.MenigaBaseConverter) MenigaOfferConverter(com.meniga.sdk.converters.MenigaOfferConverter) MenigaCategoryConverter(com.meniga.sdk.converters.MenigaCategoryConverter) MenigaRedemptionConverter(com.meniga.sdk.converters.MenigaRedemptionConverter)

Example 38 with Interceptor

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();
}
Also used : StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) Interceptor(okhttp3.Interceptor) Request(okhttp3.Request) Cache(okhttp3.Cache) BaseApplication(com.xiecc.seeWeather.base.BaseApplication) C(com.xiecc.seeWeather.common.C) CacheControl(okhttp3.CacheControl) Consumer(io.reactivex.functions.Consumer) CityORM(com.xiecc.seeWeather.modules.main.domain.CityORM) File(java.io.File) Retrofit(retrofit2.Retrofit) RxJava2CallAdapterFactory(retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory) BuildConfig(com.xiecc.seeWeather.BuildConfig) TimeUnit(java.util.concurrent.TimeUnit) ToastUtil(com.xiecc.seeWeather.common.utils.ToastUtil) OkHttpClient(okhttp3.OkHttpClient) Util(com.xiecc.seeWeather.common.utils.Util) RxUtil(com.xiecc.seeWeather.common.utils.RxUtil) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) Version(com.xiecc.seeWeather.modules.about.domain.Version) Response(okhttp3.Response) Observable(io.reactivex.Observable) WhereBuilder(com.litesuits.orm.db.assit.WhereBuilder) Weather(com.xiecc.seeWeather.modules.main.domain.Weather) Response(okhttp3.Response) OkHttpClient(okhttp3.OkHttpClient) WhereBuilder(com.litesuits.orm.db.assit.WhereBuilder) Request(okhttp3.Request) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) File(java.io.File) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) Interceptor(okhttp3.Interceptor) Cache(okhttp3.Cache)

Example 39 with Interceptor

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);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor)

Example 40 with Interceptor

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;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request)

Aggregations

IOException (java.io.IOException)37 Interceptor (okhttp3.Interceptor)36 Request (okhttp3.Request)29 MockResponse (okhttp3.mockwebserver.MockResponse)29 Response (okhttp3.Response)25 Test (org.junit.Test)25 OkHttpClient (okhttp3.OkHttpClient)24 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)24 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)9 File (java.io.File)8 Retrofit (retrofit2.Retrofit)8 Provides (dagger.Provides)6 Singleton (javax.inject.Singleton)6 Cache (okhttp3.Cache)5 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)3 InterruptedIOException (java.io.InterruptedIOException)3 Call (okhttp3.Call)3 HttpUrl (okhttp3.HttpUrl)3 NonNull (android.support.annotation.NonNull)2 ANResponse (com.androidnetworking.common.ANResponse)2