Search in sources :

Example 51 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project java by kubernetes-client.

the class ApiClient method setDebugging.

/**
 * Enable/disable debugging for this API client.
 *
 * @param debugging To enable (true) or disable (false) debugging
 * @return ApiClient
 */
public ApiClient setDebugging(boolean debugging) {
    if (debugging != this.debugging) {
        if (debugging) {
            loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(Level.BODY);
            httpClient = httpClient.newBuilder().addInterceptor(loggingInterceptor).build();
        } else {
            httpClient.interceptors().remove(loggingInterceptor);
            loggingInterceptor = null;
        }
    }
    this.debugging = debugging;
    return this;
}
Also used : HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor)

Example 52 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, HttpLoggingInterceptor loggingInterceptor, HttpCacheInterceptor cacheInterceptor, HttpHeaderInterceptor headerInterceptor, HttpProgressInterceptor progressInterceptor) {
    if (httpConfig.getConnectTimeout() > 0) {
        builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.getReadTimeout() > 0) {
        builder.readTimeout(httpConfig.getReadTimeout(), TimeUnit.SECONDS);
    }
    if (httpConfig.isUseLog() && !builder.interceptors().contains(loggingInterceptor)) {
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);
    }
    if (httpConfig.isUseCache() && !builder.interceptors().contains(cacheInterceptor)) {
        // 缓存目录
        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 (httpConfig.isUseCookie()) {
        if (httpConfig.isCookiePersistent()) {
            ClearableCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(application));
            builder.cookieJar(cookieJar);
        } else {
            CookieManager cookieManager = new CookieManager();
            cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            builder.cookieJar(new JavaNetCookieJar(cookieManager));
        }
    }
    headerInterceptor.setMapHeader(httpConfig.getMapHeader());
    if (!builder.interceptors().contains(headerInterceptor)) {
        builder.addInterceptor(headerInterceptor);
    }
    if (!builder.interceptors().contains(progressInterceptor)) {
        builder.addNetworkInterceptor(progressInterceptor);
    }
    // 配置SSL
    if (httpConfig.getSslSocketFactory() != null && httpConfig.getTrustManager() != null) {
        builder = builder.build().newBuilder().sslSocketFactory(httpConfig.getSslSocketFactory(), httpConfig.getTrustManager());
    }
    return builder.build();
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) ClearableCookieJar(com.ljy.devring.http.support.persistentcookiejar.ClearableCookieJar) PersistentCookieJar(com.ljy.devring.http.support.persistentcookiejar.PersistentCookieJar) SetCookieCache(com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache) SharedPrefsCookiePersistor(com.ljy.devring.http.support.persistentcookiejar.persistence.SharedPrefsCookiePersistor) File(java.io.File) CookieManager(java.net.CookieManager) Cache(okhttp3.Cache) DiskCache(com.ljy.devring.cache.support.DiskCache) SpCache(com.ljy.devring.cache.support.SpCache) SetCookieCache(com.ljy.devring.http.support.persistentcookiejar.cache.SetCookieCache) MemoryCache(com.ljy.devring.cache.support.MemoryCache) Provides(dagger.Provides)

Example 53 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project instagram-java-scraper by postaddictme.

the class AnonymousInstaTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new FakeBrowserInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    client = new Instagram(httpClient);
    client.basePage();
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) FakeBrowserInterceptor(me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

Example 54 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project instagram-java-scraper by postaddictme.

the class MultiThreadTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new FakeBrowserInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).cookieJar(new DefaultCookieJar(new CookieHashSet())).build();
    client = new Instagram(httpClient);
    client.basePage();
}
Also used : CookieHashSet(me.postaddict.instagram.scraper.cookie.CookieHashSet) OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) DefaultCookieJar(me.postaddict.instagram.scraper.cookie.DefaultCookieJar) FakeBrowserInterceptor(me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

Example 55 with HttpLoggingInterceptor

use of okhttp3.logging.HttpLoggingInterceptor in project instagram-java-scraper by postaddictme.

the class StatelessInstaTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    OkHttpClient httpClient = new OkHttpClient.Builder().addNetworkInterceptor(loggingInterceptor).addInterceptor(new FakeBrowserInterceptor(UserAgents.OSX_CHROME)).addInterceptor(new ErrorInterceptor()).build();
    client = new Instagram(httpClient);
    client.basePage();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ErrorInterceptor(me.postaddict.instagram.scraper.interceptor.ErrorInterceptor) FakeBrowserInterceptor(me.postaddict.instagram.scraper.interceptor.FakeBrowserInterceptor) HttpLoggingInterceptor(okhttp3.logging.HttpLoggingInterceptor) BeforeClass(org.junit.BeforeClass)

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