Search in sources :

Example 61 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor 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 62 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project coins-android by bubelov.

the class MainModule method httpClient.

@Provides
@Singleton
OkHttpClient httpClient() {
    OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS);
    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
        httpClientBuilder.addInterceptor(loggingInterceptor);
        httpClientBuilder.addNetworkInterceptor(new StethoInterceptor());
    }
    return httpClientBuilder.build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) StethoInterceptor(com.facebook.stetho.okhttp3.StethoInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 63 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project iNGAGE by davis123123.

the class UserRecentCommentHandler method enqueue.

// get all recent comments for each room, for the user
public void enqueue(String username) {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(logging);
    Retrofit retrofit = new Retrofit.Builder().client(httpClient.build()).addConverterFactory(GsonConverterFactory.create()).baseUrl(get_url).build();
    Interface service = retrofit.create(Interface.class);
    Call<ResponseBody> call = service.get(username);
    call.enqueue(new Callback<ResponseBody>() {

        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.i("STATE", "Retrofit response code: " + response.code());
            if (response.isSuccessful()) {
                Log.i("STATE", "Retrofit POST Success");
                try {
                    serverResponse = response.body().string();
                    createCommentsList(serverResponse);
                    callBackData.notifyChange();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i("Retrofit Error Code:", String.valueOf(response.code()));
                Log.i("Retrofit Error Body", response.errorBody().toString());
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.i("STATE", "Retrofit Failure");
        }
    });
}
Also used : OkHttpClient(okhttp3.OkHttpClient) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Retrofit(retrofit2.Retrofit) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 64 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project instructure-android by instructure.

the class CanvasRestAdapter method buildRollCallAdapter.

public Retrofit buildRollCallAdapter(@NonNull String url) {
    final Gson gson = new GsonBuilder().setLenient().create();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(DEBUG ? HttpLoggingInterceptor.Level.HEADERS : HttpLoggingInterceptor.Level.NONE);
    return new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create(gson)).client(new OkHttpClient.Builder().addInterceptor(loggingInterceptor).addInterceptor(new RollCallInterceptor()).readTimeout(TIMEOUT_IN_SECONDS, TimeUnit.SECONDS).dispatcher(mDispatcher).build()).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 65 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project DevRing by LJYcoder.

the class RingModule method okHttpClient.

@Singleton
@Provides
OkHttpClient okHttpClient(Application application, OkHttpClient.Builder builder, HttpConfig httpConfig, HttpProgressInterceptor progressInterceptor) {
    if (httpConfig.getConnectTimeout() > 0) {
        builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.isUseLog()) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    if (httpConfig.isUseCache()) {
        HttpCacheInterceptor cacheInterceptor = new HttpCacheInterceptor(application, httpConfig.getCacheTimeWithNet(), httpConfig.getCacheTimeWithoutNet());
        // 缓存目录
        File cacheFile;
        if (httpConfig.getCacheFolder() != null && httpConfig.getCacheFolder().isDirectory()) {
            cacheFile = httpConfig.getCacheFolder();
        } else {
            cacheFile = FileUtil.getDirectory(FileUtil.getExternalCacheDir(application), "retrofit_http_cache");
        }
        // 大小默认20Mb
        Cache cache = new Cache(cacheFile, httpConfig.getCacheSize() > 0 ? httpConfig.getCacheSize() : 1024 * 1024 * 20);
        builder.addInterceptor(cacheInterceptor);
        builder.addNetworkInterceptor(cacheInterceptor);
        builder.cache(cache);
    }
    if (!CollectionUtil.isEmpty(httpConfig.getMapHeader())) {
        HttpHeaderInterceptor headerInterceptor = new HttpHeaderInterceptor(httpConfig.getMapHeader());
        builder.addInterceptor(headerInterceptor);
    }
    builder.addNetworkInterceptor(progressInterceptor);
    return builder.build();
}
Also used : HttpHeaderInterceptor(com.ljy.devring.http.support.interceptor.HttpHeaderInterceptor) HttpCacheInterceptor(com.ljy.devring.http.support.interceptor.HttpCacheInterceptor) File(java.io.File) HttpLoggingInterceptor(com.ljy.devring.http.support.interceptor.HttpLoggingInterceptor) Cache(okhttp3.Cache) DiskCache(com.ljy.devring.cache.support.DiskCache) SpCache(com.ljy.devring.cache.support.SpCache) MemoryCache(com.ljy.devring.cache.support.MemoryCache) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)107 OkHttpClient (okhttp3.OkHttpClient)87 Provides (dagger.Provides)27 Retrofit (retrofit2.Retrofit)25 GsonBuilder (com.google.gson.GsonBuilder)24 Singleton (javax.inject.Singleton)24 Cache (okhttp3.Cache)19 Request (okhttp3.Request)16 File (java.io.File)15 Response (okhttp3.Response)15 IOException (java.io.IOException)14 Gson (com.google.gson.Gson)13 Interceptor (okhttp3.Interceptor)11 StethoInterceptor (com.facebook.stetho.okhttp3.StethoInterceptor)6 LoggingInterceptor (com.toshi.manager.network.interceptor.LoggingInterceptor)6 List (java.util.List)5 ErrorInterceptor (me.postaddict.instagram.scraper.interceptor.ErrorInterceptor)5 FakeBrowserInterceptor (me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor)5 ResponseBody (okhttp3.ResponseBody)5 BeforeClass (org.junit.BeforeClass)5