Search in sources :

Example 41 with Interceptor

use of okhttp3.Interceptor in project CircleDemo by Naoki2015.

the class VideoListGlideModule method createInterceptor.

private static Interceptor createInterceptor(final ResponseProgressListener listener) {
    return new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            Response response = chain.proceed(request);
            return response.newBuilder().body(new OkHttpProgressResponseBody(request.url(), response.body(), listener)).build();
        }
    };
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) Interceptor(okhttp3.Interceptor)

Example 42 with Interceptor

use of okhttp3.Interceptor in project reark by reark.

the class NetworkModule method provideOkHttpClient.

@Provides
@Singleton
public OkHttpClient provideOkHttpClient(@Named("networkInterceptors") List<Interceptor> networkInterceptors) {
    OkHttpClient.Builder okBuilder = new OkHttpClient().newBuilder();
    okBuilder.networkInterceptors().addAll(networkInterceptors);
    return okBuilder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 43 with Interceptor

use of okhttp3.Interceptor in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2InternalNetworking method generateDownloadObservable.

public static <T> Observable<T> generateDownloadObservable(final Rx2ANRequest request) {
    Request okHttpRequest;
    Request.Builder builder = new Request.Builder().url(request.getUrl());
    InternalNetworking.addHeadersToRequestBuilder(builder, request);
    builder = builder.get();
    if (request.getCacheControl() != null) {
        builder.cacheControl(request.getCacheControl());
    }
    okHttpRequest = builder.build();
    OkHttpClient okHttpClient;
    if (request.getOkHttpClient() != null) {
        okHttpClient = request.getOkHttpClient().newBuilder().cache(InternalNetworking.sHttpClient.cache()).addNetworkInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
            }
        }).build();
    } else {
        okHttpClient = InternalNetworking.sHttpClient.newBuilder().addNetworkInterceptor(new Interceptor() {

            @Override
            public Response intercept(Chain chain) throws IOException {
                Response originalResponse = chain.proceed(chain.request());
                return originalResponse.newBuilder().body(new ResponseProgressBody(originalResponse.body(), request.getDownloadProgressListener())).build();
            }
        }).build();
    }
    request.setCall(okHttpClient.newCall(okHttpRequest));
    return new DownloadANObservable<>(request);
}
Also used : Response(okhttp3.Response) ANResponse(com.androidnetworking.common.ANResponse) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) ResponseProgressBody(com.androidnetworking.internal.ResponseProgressBody) Interceptor(okhttp3.Interceptor)

Example 44 with Interceptor

use of okhttp3.Interceptor in project bilibili-android-client by HotBitmapGG.

the class RetrofitHelper method initOkHttpClient.

/**
   * 初始化OKHttpClient,设置缓存,设置超时时间,设置打印日志,设置UA拦截器
   */
private static void initOkHttpClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    if (mOkHttpClient == null) {
        synchronized (RetrofitHelper.class) {
            if (mOkHttpClient == null) {
                //设置Http缓存
                Cache cache = new Cache(new File(BilibiliApp.getInstance().getCacheDir(), "HttpCache"), 1024 * 1024 * 10);
                mOkHttpClient = new OkHttpClient.Builder().cache(cache).addInterceptor(interceptor).addNetworkInterceptor(new CacheInterceptor()).addNetworkInterceptor(new StethoInterceptor()).retryOnConnectionFailure(true).connectTimeout(30, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(new UserAgentInterceptor()).build();
            }
        }
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) File(java.io.File) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache)

Example 45 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class RealCall method getResponseWithInterceptorChain.

Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    interceptors.add(retryAndFollowUpInterceptor);
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    interceptors.add(new CacheInterceptor(client.internalCache()));
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
        interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(forWebSocket));
    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0, originalRequest);
    return chain.proceed(originalRequest);
}
Also used : ConnectInterceptor(okhttp3.internal.connection.ConnectInterceptor) RealInterceptorChain(okhttp3.internal.http.RealInterceptorChain) ArrayList(java.util.ArrayList) CallServerInterceptor(okhttp3.internal.http.CallServerInterceptor) BridgeInterceptor(okhttp3.internal.http.BridgeInterceptor) CacheInterceptor(okhttp3.internal.cache.CacheInterceptor) ConnectInterceptor(okhttp3.internal.connection.ConnectInterceptor) RetryAndFollowUpInterceptor(okhttp3.internal.http.RetryAndFollowUpInterceptor) CallServerInterceptor(okhttp3.internal.http.CallServerInterceptor) BridgeInterceptor(okhttp3.internal.http.BridgeInterceptor) CacheInterceptor(okhttp3.internal.cache.CacheInterceptor)

Aggregations

IOException (java.io.IOException)35 Interceptor (okhttp3.Interceptor)33 MockResponse (okhttp3.mockwebserver.MockResponse)29 Request (okhttp3.Request)26 Test (org.junit.Test)25 Response (okhttp3.Response)24 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)24 OkHttpClient (okhttp3.OkHttpClient)20 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)10 File (java.io.File)8 Provides (dagger.Provides)6 Singleton (javax.inject.Singleton)6 Cache (okhttp3.Cache)4 Retrofit (retrofit2.Retrofit)4 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 ResponseProgressBody (com.androidnetworking.internal.ResponseProgressBody)2