Java Code Examples for okhttp3.logging.HttpLoggingInterceptor#setLevel()

The following examples show how to use okhttp3.logging.HttpLoggingInterceptor#setLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientAPI.java    From OAuth-2.0-Cookbook with MIT License 6 votes vote down vote up
private ClientAPI() {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.addInterceptor(logging);
    client.addInterceptor(new ErrorInterceptor());
    client.addInterceptor(new BearerTokenHeaderInterceptor());

    retrofit = new Retrofit.Builder()
            .baseUrl("http://" + BASE_URL)
            .addConverterFactory(JacksonConverterFactory.create())
            .client(client.build())
            .build();

}
 
Example 2
Source File: ApiClient.java    From android with MIT License 6 votes vote down vote up
/**
     * Get the Prkng API service
     *
     * @param httpLogging Enable verbose
     * @return PrkngService
     */
    private static PrkngService getService(boolean httpLogging) {
        final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(httpLogging ? HttpLoggingInterceptor.Level.BODY :
                HttpLoggingInterceptor.Level.NONE);

        final OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(new HttpErrorInterceptor())
                .addInterceptor(interceptor)
                .build();

        final Gson gson = new GsonBuilder()
                .setDateFormat(CalendarUtils.DATE_FORMAT_ISO_8601)
                .create();

        final Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.API_BASE_URL)
                .client(client)
//                .addConverterFactory(LatLngConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        return retrofit.create(PrkngService.class);
    }
 
Example 3
Source File: OkHttpEngine.java    From DanDanPlayForAndroid with MIT License 6 votes vote down vote up
private OkHttpClient initOkHttpConfig() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.connectTimeout(30, TimeUnit.SECONDS);
    builder.readTimeout(30, TimeUnit.SECONDS);
    builder.writeTimeout(30, TimeUnit.SECONDS);
    builder.retryOnConnectionFailure(true);
    File cacheDir = new File(IApplication.get_context().getCacheDir(), "HttpResponseCache");
    builder.cache(new Cache(cacheDir, 10 * 1024 * 1024));
    builder.cookieJar(IApplication.getCookiesManager());
    if (IApplication.isDebug()) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(interceptor);
    }
    return builder.build();
}
 
Example 4
Source File: HttpMethods.java    From Dictionary with Apache License 2.0 6 votes vote down vote up
private HttpMethods() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();
    Retrofit retrofit1 = new Retrofit.Builder()
            .client(client)
            .baseUrl("http://fanyi.youdao.com")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    Retrofit retrofit2 = new Retrofit.Builder()
            .client(client)
            .baseUrl("http://open.iciba.com")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    Retrofit retrofit3 = new Retrofit.Builder()
            .client(client)
            .baseUrl("http://dict-co.iciba.com")
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    service1 = retrofit1.create(ApiService.class);
    service2 = retrofit2.create(ApiService.class);
    service3 = retrofit3.create(ApiService.class);
}
 
Example 5
Source File: RetrofitModule.java    From Nibo with MIT License 6 votes vote down vote up
private RetrofitModule() {
    if (retrofit == null) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);


        retrofit = new Retrofit.Builder()
                .baseUrl(NiboConstants.BASE_DIRECTIONS_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();
    }
}
 
Example 6
Source File: RetrofitHelper.java    From gank.io-unofficial-android-client with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化OKHttpClient
 */
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(GankApp.getContext().getCacheDir(), "HttpCache"),
            1024 * 1024 * 100);

        mOkHttpClient = new OkHttpClient.Builder()
            .cache(cache)
            .addInterceptor(interceptor)
            .addNetworkInterceptor(new StethoInterceptor())
            .retryOnConnectionFailure(true)
            .connectTimeout(15, TimeUnit.SECONDS)
            .build();
      }
    }
  }
}
 
Example 7
Source File: MapboxSpeech.java    From mapbox-java with MIT License 6 votes vote down vote up
@Override
public synchronized OkHttpClient getOkHttpClient() {
  if (okHttpClient == null) {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    if (isEnableDebug()) {
      HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
      logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
      httpClient.addInterceptor(logging);
    }
    if (cache() != null) {
      httpClient.cache(cache());
    }
    if (interceptor() != null) {
      httpClient.addInterceptor(interceptor());
    }
    if (networkInterceptor() != null) {
      httpClient.addNetworkInterceptor(networkInterceptor());
    }

    okHttpClient = httpClient.build();
  }
  return okHttpClient;
}
 
Example 8
Source File: TestOmdb.java    From kripton with Apache License 2.0 6 votes vote down vote up
/**
 * Uncomment @Test to run
 */
@Test
public void test() throws IOException {
	String apiKey = "d497e644";

	HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
	logging.setLevel(HttpLoggingInterceptor.Level.BODY);

	OkHttpClient.Builder httpClient = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
			.addInterceptor(logging);

	Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.omdbapi.com/")
			.addConverterFactory(KriptonBinderConverterFactory.create())
			.client(httpClient.build()).build();

	OmdbApi service = retrofit.create(OmdbApi.class);
	Response<Search> response = service.search("avengers", apiKey).execute();

	log(response.body().getTotalResults());

}
 
Example 9
Source File: ApiModule.java    From AndroidArchitecture with Apache License 2.0 6 votes vote down vote up
@Provides
@Singleton
public OkHttpClient provideOkHttpClient() {
    final OkHttpClient.Builder builder = new OkHttpClient.Builder();

    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(logging);
    }

    builder.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
            .readTimeout(60 * 1000, TimeUnit.MILLISECONDS);

    return builder.build();
}
 
Example 10
Source File: Example11.java    From AnDevCon-RxPatterns with Apache License 2.0 6 votes vote down vote up
private void createGithubClient() {
    if (mGitHubClient == null) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .addInterceptor(chain ->
                        chain.proceed(chain.request().newBuilder().addHeader("Authorization",
                                "token " + Secrets.GITHUB_PERSONAL_ACCESS_TOKEN).build())).build();

        mGitHubClient = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .baseUrl(GITHUB_BASE_URL)
                .build()
                .create(GitHubClient.class);
    }
}
 
Example 11
Source File: MapboxRouteTiles.java    From mapbox-java with MIT License 6 votes vote down vote up
@Override
protected synchronized OkHttpClient getOkHttpClient() {
  if (okHttpClient == null) {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    if (isEnableDebug()) {
      HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
      logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
      httpClient.addInterceptor(logging);
    }
    Interceptor interceptor = interceptor();
    if (interceptor != null) {
      httpClient.addInterceptor(interceptor);
    }
    Interceptor networkInterceptor = networkInterceptor();
    if (networkInterceptor != null) {
      httpClient.addNetworkInterceptor(networkInterceptor);
    }

    okHttpClient = httpClient.build();
  }
  return okHttpClient;
}
 
Example 12
Source File: DataModule.java    From vb-android-app-quality with Apache License 2.0 5 votes vote down vote up
/**
 * Return the OkHttpClient implementation used by this app.
 *
 * @return the OkHttpClient implementation used by this app.
 */
@Provides
@Singleton
public OkHttpClient provideOkHttpClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    return new OkHttpClient.Builder().addInterceptor(interceptor).build();
}
 
Example 13
Source File: DebugApiModule.java    From u2020-mvp with Apache License 2.0 5 votes vote down vote up
@Provides
@ApplicationScope
HttpLoggingInterceptor provideLoggingInterceptor() {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(message -> Timber.tag("OkHttp").v(message));
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
    return loggingInterceptor;
}
 
Example 14
Source File: OkHttpManager.java    From Android-Architecture with Apache License 2.0 5 votes vote down vote up
private OkHttpClient getOkHttpClient() {
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BASIC : HttpLoggingInterceptor.Level.NONE);
    if (mOkHttpClient == null) {
        mOkHttpClient = new OkHttpClient.Builder().addInterceptor(loggingInterceptor).retryOnConnectionFailure(true)
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS).build();
    }
    return mOkHttpClient;
}
 
Example 15
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: Injection.java    From GithubUsersSearchApp with Apache License 2.0 5 votes vote down vote up
static OkHttpClient getOkHttpClient() {
    if (okHttpClient == null) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
        okHttpClient = new OkHttpClient.Builder().addInterceptor(logging).build();
    }

    return okHttpClient;
}
 
Example 17
Source File: ReadModel.java    From Flora with MIT License 5 votes vote down vote up
public void getNews() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Config.BASE_GANK)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(okHttpClient)
            .build();

    NewsService newsService = retrofit.create(NewsService.class);
    newsService.getTodayNews()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<NewsResponse>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    if (null != callback) {
                        callback.onFail(e.getMessage());
                    }
                }

                @Override
                public void onNext(NewsResponse newsResponse) {
                    if (null != callback) {
                        callback.onSuccess(newsResponse);
                    }
                }
            });
}
 
Example 18
Source File: ApiClient.java    From eve-esi with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: HttpModule.java    From Ency with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
OkHttpClient provideOkHttpClient(OkHttpClient.Builder builder, final Context context) {
    if (BuildConfig.DEBUG) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        builder.addInterceptor(loggingInterceptor);
    }
    File cacheFile = new File(Constants.PATH_CACHE);
    Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
    Interceptor cacheInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!AppNetWorkUtil.isNetworkConnected(context)) {
                request = request.newBuilder()
                        .cacheControl(CacheControl.FORCE_CACHE)
                        .build();
            }
            Response response = chain.proceed(request);
            if (AppNetWorkUtil.isNetworkConnected(context)) {
                int maxAge = 0;
                // 有网络时, 不缓存, 最大保存时长为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;
        }
    };
    //设置缓存
    builder.addNetworkInterceptor(cacheInterceptor);
    builder.addInterceptor(cacheInterceptor);
    builder.cache(cache);
    //设置超时
    builder.connectTimeout(10, TimeUnit.SECONDS);
    builder.readTimeout(20, TimeUnit.SECONDS);
    builder.writeTimeout(20, TimeUnit.SECONDS);
    //错误重连
    builder.retryOnConnectionFailure(true);
    return builder.build();
}
 
Example 20
Source File: OneNetApi.java    From AndroidSDK with MIT License 4 votes vote down vote up
/**
 * 初始化SDK
 *
 * @param application Application实例
 * @param debug       是否开启调试模式(打印 HTTP 请求和响应日志)
 * @param config      HTTP 相关配置(超时时间,重试次数等)
 */
public static void init(Application application, boolean debug, Config config) {
    try {
        sAppKey = Meta.readAppKey(application);
        String scheme = Meta.readScheme(application);
        if (!TextUtils.isEmpty(scheme) && !scheme.contains(" ")) {
            Urls.sScheme = scheme;
        }
        String host = Meta.readHost(application);
        if (!TextUtils.isEmpty(host) && !host.contains(" ")) {
            Urls.sHost = host;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    sDebug = debug;

    OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();

    if (sDebug) {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new OneNetLogger());
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClientBuilder.addNetworkInterceptor(loggingInterceptor);
    }
    okHttpClientBuilder.addInterceptor(sApiKeyInterceptor);

    if (config != null) {
        if (config.connectTimeout() > 0) {
            okHttpClientBuilder.connectTimeout(config.connectTimeout(), TimeUnit.MILLISECONDS);
        }
        if (config.readTimeout() > 0) {
            okHttpClientBuilder.readTimeout(config.readTimeout(), TimeUnit.MILLISECONDS);
        }
        if (config.writeTimeout() > 0) {
            okHttpClientBuilder.writeTimeout(config.writeTimeout(), TimeUnit.MILLISECONDS);
        }
        if (config.retryCount() > 0) {
            okHttpClientBuilder.addInterceptor(new RetryInterceptor(config.retryCount()));
        }
    }

    OkHttpClient client = okHttpClientBuilder.build();
    sHttpExecutor = new HttpExecutor(client);
}