Search in sources :

Example 1 with StethoInterceptor

use of com.facebook.stetho.okhttp3.StethoInterceptor in project jianshi by wingjay.

the class AppModule method provideOkHttpClient.

@Provides
@Singleton
OkHttpClient provideOkHttpClient(GlobalRequestInterceptor globalRequestInterceptor) {
    OkHttpClient.Builder builder = new OkHttpClient.Builder().connectionPool(new ConnectionPool(5, 59, TimeUnit.SECONDS)).connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).addInterceptor(globalRequestInterceptor).retryOnConnectionFailure(false);
    if (BuildConfig.DEBUG) {
        builder.addNetworkInterceptor(new StethoInterceptor());
    }
    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    httpLoggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
    builder.addInterceptor(httpLoggingInterceptor);
    return builder.build();
}
Also used : ConnectionPool(okhttp3.ConnectionPool) OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 2 with StethoInterceptor

use of com.facebook.stetho.okhttp3.StethoInterceptor in project fresco by facebook.

the class ImagePipelineConfigFactory method getOkHttpImagePipelineConfig.

/**
   * Creates config using OkHttp as network backed.
   */
public static ImagePipelineConfig getOkHttpImagePipelineConfig(Context context) {
    if (sOkHttpImagePipelineConfig == null) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();
        ImagePipelineConfig.Builder configBuilder = OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient);
        configureCaches(configBuilder, context);
        configureLoggingListeners(configBuilder);
        sOkHttpImagePipelineConfig = configBuilder.build();
    }
    return sOkHttpImagePipelineConfig;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ImagePipelineConfig(com.facebook.imagepipeline.core.ImagePipelineConfig) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor)

Example 3 with StethoInterceptor

use of com.facebook.stetho.okhttp3.StethoInterceptor 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 4 with StethoInterceptor

use of com.facebook.stetho.okhttp3.StethoInterceptor in project 2017-01-HUDI-MAC-CHAR by NHNNEXT.

the class ServiceGenerator method createService.

public static <S> S createService(Class<S> serviceClass, Context context) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    if (BuildConfig.DEBUG) {
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    } else {
        logging.setLevel(HttpLoggingInterceptor.Level.NONE);
    }
    PersistentCookieStore cookieStore = new PersistentCookieStore(context);
    java.net.CookieManager cookieManager = new java.net.CookieManager(cookieStore, CookiePolicy.ACCEPT_ALL);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder().cookieJar(new JavaNetCookieJar(cookieManager)).addInterceptor(logging).addNetworkInterceptor(new StethoInterceptor());
    Gson gson = new GsonBuilder().setLenient().create();
    Retrofit retorfit = new Retrofit.Builder().baseUrl(MafiaRemoteService.BASE_URL).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build()).build();
    return retorfit.create(serviceClass);
}
Also used : PersistentCookieStore(com.zimincom.mafiaonline.PersistentCookieStore) JavaNetCookieJar(okhttp3.internal.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Retrofit(retrofit2.Retrofit) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 5 with StethoInterceptor

use of com.facebook.stetho.okhttp3.StethoInterceptor in project Atom_Android by Rogrand-Dev.

the class HttpModule method provideClient.

@Singleton
@Provides
OkHttpClient provideClient(OkHttpClient.Builder builder) {
    if (DemoApp.getAppContext().isDebug()) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {

            @Override
            public void log(String message) {
                LogUtils.i(TAG, message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        // 设置 Log 拦截器
        builder.addInterceptor(loggingInterceptor);
    }
    File cacheFile = new File(DataUtils.getCacheFile(DemoApp.getAppContext()), "RograndCache");
    // 100 MB
    int cacheSize = 100 * 1024 * 1024;
    Cache cache = new Cache(cacheFile, cacheSize);
    Interceptor cacheInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            CacheControl.Builder cacheBuilder = new CacheControl.Builder();
            cacheBuilder.maxAge(0, TimeUnit.SECONDS);
            cacheBuilder.maxStale(365, TimeUnit.DAYS);
            CacheControl cacheControl = cacheBuilder.build();
            Request request = chain.request();
            if (!NetUtils.isNetworkAvailable(DemoApp.getAppContext())) {
                request = request.newBuilder().cacheControl(cacheControl).build();
            }
            Response response = chain.proceed(request);
            if (NetUtils.isNetworkAvailable(DemoApp.getAppContext())) {
                // 有网络时, 不缓存, 最大保存时长为0
                int maxAge = 0;
                response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
            } else {
                // 无网络时,设置超时为4周
                int maxStale = 60 * 60 * 24 * 28;
                response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
            }
            return response;
        }
    };
    // 设置 header(设置 Token、API 版本号)
    Interceptor headerInterceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            request = request.newBuilder().addHeader("Authorization", "Bearer " + SPUtils.get(DemoApp.getAppContext(), Constants.ACCESS_TOKEN, "")).addHeader("api_version", UrlConstants.API_VERSION).build();
            return chain.proceed(request);
        }
    };
    // 设置统一的请求头部参数
    builder.addInterceptor(headerInterceptor);
    // 设置缓存
    builder.addNetworkInterceptor(cacheInterceptor);
    builder.addInterceptor(cacheInterceptor);
    builder.cache(cache);
    // 设置连接超时
    builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置读取数据超时
    builder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置写入数据超时
    builder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
    // 设置 Stetho
    builder.addNetworkInterceptor(new StethoInterceptor());
    // 设置 Cookie
    builder.cookieJar(new CookieManger(DemoApp.getAppContext()));
    // 错误重连
    builder.retryOnConnectionFailure(true);
    return builder.build();
}
Also used : CookieManger(com.rogrand.demo.http.cookie.CookieManger) Request(okhttp3.Request) Response(okhttp3.Response) CacheControl(okhttp3.CacheControl) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) File(java.io.File) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) Interceptor(okhttp3.Interceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Cache(okhttp3.Cache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)6 OkHttpClient (okhttp3.OkHttpClient)5 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)5 Provides (dagger.Provides)3 Singleton (javax.inject.Singleton)3 File (java.io.File)2 Cache (okhttp3.Cache)2 ImagePipelineConfig (com.facebook.imagepipeline.core.ImagePipelineConfig)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 CookieManger (com.rogrand.demo.http.cookie.CookieManger)1 PersistentCookieStore (com.zimincom.mafiaonline.PersistentCookieStore)1 CacheControl (okhttp3.CacheControl)1 ConnectionPool (okhttp3.ConnectionPool)1 Interceptor (okhttp3.Interceptor)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 JavaNetCookieJar (okhttp3.internal.JavaNetCookieJar)1 Retrofit (retrofit2.Retrofit)1