Search in sources :

Example 71 with Interceptor

use of okhttp3.Interceptor in project smartmodule by carozhu.

the class HttpDownloadHelper method creatService.

public <S> S creatService(Class<S> serviceClass, final ProgressListener progressListener) {
    //给httpClient添加拦截器
    httpClient.addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            okhttp3.Response originalResponse = chain.proceed(chain.request());
            ProgressResponseBody responseBody = new ProgressResponseBody(originalResponse.body(), progressListener);
            return originalResponse.newBuilder().body(responseBody).build();
        }
    });
    Retrofit retrofit = builder.build();
    return retrofit.create(serviceClass);
}
Also used : Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor)

Example 72 with Interceptor

use of okhttp3.Interceptor in project Varis-Android by dkhmelenko.

the class NetworkModule method okHttpClient.

@Provides
@Singleton
public static OkHttpClient okHttpClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    return new OkHttpClient.Builder().addInterceptor(interceptor).followRedirects(false).followSslRedirects(false).build();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 73 with Interceptor

use of okhttp3.Interceptor in project run-wallet-android by runplay.

the class IotaMsgCore method postConstruct.

/**
 * added header for IRI
 */
private void postConstruct() {
    final String nodeUrl = protocol + "://" + host + ":" + port;
    // Create OkHttpBuilder
    Authenticator auth = new Authenticator() {

        @Nullable
        @Override
        public Request authenticate(Route route, okhttp3.Response response) throws IOException {
            if (responseCount(response) >= 3) {
                // If we've failed 3 times, give up. - in real life, never give up!!
                return null;
            }
            String credential = Credentials.basic(uname, upassword);
            return response.request().newBuilder().header("Authorization", credential).build();
        }
    };
    final OkHttpClient client = new OkHttpClient.Builder().readTimeout(5000, TimeUnit.SECONDS).authenticator(auth).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);
    log.debug("Jota-API Java proxy pointing to node url: '{}'", nodeUrl);
}
Also used : IotaAPIService(jota.IotaAPIService) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Interceptor(okhttp3.Interceptor) Authenticator(okhttp3.Authenticator) Route(okhttp3.Route)

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