private static <T> T initWeatherService(String baseUrl, Class<T> clazz) { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); if (BuildConfig.DEBUG) { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(httpLoggingInterceptor).addNetworkInterceptor(new StethoInterceptor()); } OkHttpClient client = builder.build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(FastJsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build(); return retrofit.create(clazz); }
private static Retrofit create() { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); builder.readTimeout(10, TimeUnit.SECONDS); builder.connectTimeout(9, TimeUnit.SECONDS); builder.addNetworkInterceptor(new StethoInterceptor()); if (BuildConfig.DEBUG) { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); builder.addInterceptor(interceptor); } return new Retrofit.Builder().baseUrl(ENDPOINT) .client(builder.build()) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); }
@SuppressLint("AuthLeak") public static Retrofit getInstance(Context context) { if (httpClient == null) { httpClient = new OkHttpClient.Builder(); } if (retrofit == null) { httpClient.addNetworkInterceptor(new StethoInterceptor()) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .addInterceptor(provideOfflineCacheInterceptor(context)) .addNetworkInterceptor(provideCacheInterceptor()) .cache(provideCache(context)); retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .client(httpClient.build()) .build(); } return retrofit; }
@SuppressLint("AuthLeak") public static Retrofit getInstance(Context context) { if (httpClient == null) { httpClient = new OkHttpClient.Builder(); } if (retrofit == null) { httpClient.addNetworkInterceptor(new AuthenticationInterceptor(Credentials.basic(apiKey, "x"))) .addNetworkInterceptor(new StethoInterceptor()) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .addInterceptor(provideOfflineCacheInterceptor(context)) .addNetworkInterceptor(provideCacheInterceptor()) .cache(provideCache(context)); retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .client(httpClient.build()) .build(); } return retrofit; }
@Provides @Singleton Retrofit provideCall() { OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer(App.getTwitterKey(), App.getTwitterSecret()); consumer.setTokenWithSecret( App.getApplicationInstance().getTwitterSession().getAuthToken().token, App.getApplicationInstance().getTwitterSession().getAuthToken().secret); Retrofit.Builder builder = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new SigningInterceptor(consumer)) .addNetworkInterceptor(new StethoInterceptor()) .build(); return builder.client(client).build(); }
public HttpRequestFactory() { HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); logging.setLevel(HttpLoggingInterceptor.Level.HEADERS); OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(TIMEOUT, TimeUnit.SECONDS) .readTimeout(TIMEOUT, TimeUnit.SECONDS) .writeTimeout(TIMEOUT, TimeUnit.SECONDS) .cache(getCache()) .addInterceptor(logging) .addNetworkInterceptor(mTokenInterceptor) //应用拦截器,用于离线缓存 .addInterceptor(mCacheInterceptor) //网络拦截器,用于在线缓存 .addNetworkInterceptor(mCacheInterceptor) .addNetworkInterceptor(new StethoInterceptor()) .build(); Retrofit retrofit = new Retrofit.Builder().baseUrl(Constants.FanFou.FANFOU_API_URL) .addConverterFactory(ResponseConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient) .build(); mServiceFactory = retrofit.create(ApiFactory.class); }
@Override public void onCreate() { super.onCreate(); if (BuildConfig.DEBUG){ Stetho.initializeWithDefaults(this); } ApiRESTful apiRESTful = ApiRESTful.getApiRESTful(this, false); apiRESTful.setModelUrl(AppConfig.HOST); apiRESTful.setPushWs(""); apiRESTful.setAutoToWs(false); apiRESTful.setCardId("04df-ee031b69-0000-4e02-8a56-41736fc1226a-e0000000"); apiRESTful.setHeadersPrefix("x-hz-"); OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.addNetworkInterceptor(new StethoInterceptor()); //apiRESTful.setOKHttpClientBuider(builder); ALog.debug = true; }
/** * POST function that help to create and HTTP GET request * @param url A variable of type String * @param formBody A Variable of type Request Body * @return A Response of get request * @see Response */ static Response post(String url, RequestBody formBody){ OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder() .connectTimeout(timeout, TimeUnit.SECONDS) .readTimeout(timeout, TimeUnit.SECONDS); if(NyrisEndpoints.DEBUG) httpBuilder.networkInterceptors().add(new StethoInterceptor()); OkHttpClient mHttpClient = httpBuilder.build(); Request request = new Request.Builder() .url(url) .addHeader("User-Agent", userAgent) .post(formBody) .build(); try{ return mHttpClient.newCall(request).execute(); } catch (Exception e){ e.printStackTrace(); } return null; }
/** * POST function that help to create and HTTP POST request * @param builder A variable of type Request.Builder * @return A Response of post request * @see Response * @see Request.Builder */ static Response post(Request.Builder builder) { OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder() .connectTimeout(timeout, TimeUnit.SECONDS) .readTimeout(timeout, TimeUnit.SECONDS); if(NyrisEndpoints.DEBUG) httpBuilder.networkInterceptors().add(new StethoInterceptor()); OkHttpClient mHttpClient = httpBuilder.build(); builder.addHeader("User-Agent", userAgent); try{ return mHttpClient.newCall(builder.build()).execute(); } catch(Exception e){ e.printStackTrace(); } return null; }
GankRetrofit() { HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(interceptor) .addNetworkInterceptor(new HeaderInterceptor()) .addNetworkInterceptor(new StethoInterceptor()) .retryOnConnectionFailure(true) .connectTimeout(15, TimeUnit.SECONDS) .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://gank.io/api/") .client(client) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); gankService = retrofit.create(GankApi.class); }
/** * 初始化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(MoeQuestApp.getContext().getCacheDir(), "HttpCache"), 1024 * 1024 * 100); mOkHttpClient = new OkHttpClient.Builder() .cache(cache) .addInterceptor(interceptor) .addNetworkInterceptor(new StethoInterceptor()) .retryOnConnectionFailure(true) .connectTimeout(20, TimeUnit.SECONDS) .build(); } } } }
@Override public void registerComponents(Context context, Glide glide) { Stetho.initializeWithDefaults(context); final Cache cache = new Cache(new File(context.getCacheDir(), "okhttp"), IMAGE_CACHE_SIZE); HttpLoggingInterceptor logger = new HttpLoggingInterceptor(); // logger.setLevel(Level.BASIC); OkHttpClient client = new OkHttpClient() .newBuilder() .cache(cache) .addNetworkInterceptor(new StethoInterceptor()) .addInterceptor(logger) .build(); glide.register(CachedGlideUrl.class, InputStream.class, superFactory(new OkHttpUrlLoader.Factory(client), CachedGlideUrl.class)); glide.register(ForceLoadGlideUrl.class, InputStream.class, superFactory(new OkHttpUrlLoader.Factory(client), ForceLoadGlideUrl.class)); }
@Data @Provides @Singleton OkHttpClient providerDataOkHttpClient(@Data OkHttpClient.Builder builder) { Preconditions.checkState("debug".equals(BuildConfig.BUILD_TYPE)); //log builder.addNetworkInterceptor(new StethoInterceptor()); //trust https try { X509TrustManager trustManager = new NullTrustManager(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom()); builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); } catch (Exception e) { e.printStackTrace(); } return builder.build(); }
@Image @Provides @Singleton OkHttpClient providerImageOkHttpClient(@Image OkHttpClient.Builder builder) { Preconditions.checkState("debug".equals(BuildConfig.BUILD_TYPE)); //log builder.addNetworkInterceptor(new StethoInterceptor()); //trust https try { X509TrustManager trustManager = new NullTrustManager(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom()); builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); } catch (Exception e) { e.printStackTrace(); } return builder.build(); }
@AppData @Provides @Singleton OkHttpClient providerAppdataOkHttpClient(@AppData OkHttpClient.Builder builder) { Preconditions.checkState("debug".equals(BuildConfig.BUILD_TYPE)); //log builder.addNetworkInterceptor(new StethoInterceptor()); //trust https try { X509TrustManager trustManager = new NullTrustManager(); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom()); builder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); } catch (Exception e) { e.printStackTrace(); } return builder.build(); }
/** * 初始化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(); } } } }
@Provides @Singleton OkHttpClient okHttpClient(Cache cache, NetworkConnectionWatcher watcher) { OkHttpClient.Builder builder = new OkHttpClient.Builder() .readTimeout(15, TimeUnit.SECONDS) // api is slow when getting today's durations .cache(cache) .addInterceptor(new CacheUpgraderInterceptor(watcher)) .addInterceptor(new HttpLoggingInterceptor() .setLevel(HttpLoggingInterceptor.Level.HEADERS)); if (BuildConfig.DEBUG) { builder.addNetworkInterceptor(new StethoInterceptor()); } return builder.build(); }
/** * 每个获取 APIService 的方法都在先检测 sHttpClient 是否为空 */ private static void initHttpClient() { OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); File file = new File(App.getContext().getCacheDir(), "ResponseCache"); builder.cache(new Cache(file, AppConfig.RESPONSE_CACHE_SIZE)) .readTimeout(AppConfig.READ_TIME_OUT, TimeUnit.SECONDS) .connectTimeout(AppConfig.CONNECTION_TIME_OUT, TimeUnit.SECONDS); if (AppConfig.isDebug) { // TODO: 2016/3/19 Release 版本需要注释此拦截器 builder.addNetworkInterceptor(new StethoInterceptor()); builder.addInterceptor(LoggingInterceptor); } builder.addNetworkInterceptor(HttpInterceptor); builder.addInterceptor(HttpInterceptor); sHttpClient = builder.build(); }
@Provides @Singleton public OkHttpClient provideOkHttpClient(Application application) { final OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder(); // Install an HTTP cache in the application cache directory. File cacheDir = new File(application.getCacheDir(), HTTP_CACHE_DIR); Cache cache = new Cache(cacheDir, HTTP_DISK_CACHE_SIZE); final HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS); httpBuilder.addInterceptor(interceptor); httpBuilder.cache(cache); if (BuildConfig.DEBUG) { httpBuilder.addNetworkInterceptor(new StethoInterceptor()); } return httpBuilder.build(); }
@Provides @Singleton public OkHttpClient provideOkHttpClient(final Application application) { final OkHttpClient.Builder builder = new OkHttpClient.Builder(); if (BuildConfig.DEBUG) { builder.addInterceptor(provideHttpLoggingInterceptor()); builder.addNetworkInterceptor(new StethoInterceptor()); } builder.connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .addInterceptor(provideOfflineCacheInterceptor()) .addNetworkInterceptor(provideCacheInterceptor()) .cache(provideCache(application)); return builder.build(); }
@Provides @Singleton OkHttpClient provideOkHttpClient( @ClientCache Cache cache, CacheInterceptor cacheInterceptor, //HttpLoggingInterceptor loggingInterceptor, @Named("userAgent") String userAgentValue ) { return new OkHttpClient.Builder() .cache(cache) //.addInterceptor(loggingInterceptor) .addNetworkInterceptor(new UserAgentInterceptor(userAgentValue)) .addNetworkInterceptor(new StethoInterceptor()) .addInterceptor(cacheInterceptor) .addNetworkInterceptor(cacheInterceptor) .build(); }
@Provides @Singleton public OkHttpClient provideClient(){ return new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .addInterceptor(chain -> { final Request request = chain.request(); final Request newRequest = request.newBuilder() //ajoute "baerer: 1234567890" en header de chaque requête .addHeader("bearer","1234567890") .build(); return chain.proceed(newRequest); }) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); }
@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(); }
/** * 初始化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(HeroVideoApp.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(); } } } }
/** * 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; }
@NonNull @Provides @Singleton OkHttpClient provideOKHttpClient() { return new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .build(); }
public static void setup() { httpClient.addInterceptor(provideOfflineCacheInterceptor()); httpClient.addInterceptor(new AddCookiesInterceptor()); // VERY VERY IMPORTANT httpClient.addInterceptor(new ReceivedCookiesInterceptor()); // VERY VERY IMPORTANT httpClient.addInterceptor(provideHttpLoggingInterceptor()); httpClient.addNetworkInterceptor(new StethoInterceptor());// Stetho //httpClient.addNetworkInterceptor(provideCacheInterceptor()); httpClient.cache(provideCache()); OkHttpClient client = httpClient.build(); retrofit = builder.client(client).build(); }
@Override public void onCreate() { super.onCreate(); instance = this; Stetho.initializeWithDefaults(this); new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .build(); //setDummyData(); clearDummyData(); ServiceGenerator.setup(); Log.i("Setup" , "Complete"); }
@Singleton @Provides OkHttpClient provideOkHttpClient(OkHttpClient.Builder okHttpClientBuilder) { if(BuildConfig.DEBUG) { okHttpClientBuilder .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .addNetworkInterceptor(new StethoInterceptor()); } return okHttpClientBuilder.build(); }
/** * 初始化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(); } } } }
@Override OkHttpClient.Builder okHttpClientBuilder(Application application) { OkHttpClient.Builder builder = super.okHttpClientBuilder(application) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .addInterceptor(new StethoInterceptor()); if (new DebugPreferences(application).isChuckEnabled()) { builder.addInterceptor(new ChuckInterceptor(application)); } return builder; }
@Provides @Singleton static OkHttpClient provideOkHttpClient() { return new OkHttpClient.Builder() .readTimeout(DEFAULT_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS) .connectTimeout(DEFAULT_CONNECT_TIMEOUT_SECONDS, TimeUnit.SECONDS) .addNetworkInterceptor(new StethoInterceptor()) .build(); }
@Override public void onCreate() { super.onCreate(); INSTANCE = this; userAgent = Util.getUserAgent(this, "ExoPlayerDemo"); okHttpClient = new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build(); Stetho.initializeWithDefaults(this); }
private OkHttpClient getOkHttpClient() { OkHttpClient.Builder builder = new OkHttpClient.Builder(); // builder.addInterceptor(getLogInterceptor()); // builder.addNetworkInterceptor(getHeadOperateInterceptor()); builder.addNetworkInterceptor(new StethoInterceptor()); builder.retryOnConnectionFailure(true); builder.connectTimeout(20, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); return builder.build(); }
@Provides @Singleton OkHttpClient provideOkHttpClient(Cache cache, HttpLoggingInterceptor httpLoggingInterceptor, StethoInterceptor stethoInterceptor) { return new OkHttpClient.Builder() //.cache(cache) .addInterceptor(httpLoggingInterceptor) .addNetworkInterceptor(stethoInterceptor) //.addNetworkInterceptor(networkInterceptor) //.addInterceptor(interceptor) .build(); }
public static PyWhatAutoService newPywaService(String url) { Retrofit retrofit = new Retrofit.Builder().client( new OkHttpClient.Builder().followRedirects(false) .addNetworkInterceptor(new StethoInterceptor()) .build()) .baseUrl(url) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return retrofit.create(PyWhatAutoService.class); }
protected OkHttpClient createClient() { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(TIMEOUT_SECONDS, TimeUnit.SECONDS) .addInterceptor(new ChuckInterceptor(King.getApplicationContext())) .addNetworkInterceptor(new HttpLoggingInterceptor()) .addNetworkInterceptor(new StethoInterceptor()) .build(); return client; }
@Provides @Singleton @Named("Api") OkHttpClient provideApiClient(OkHttpClient client, HttpLoggingInterceptor loggingInterceptor) { return ApiModule.createApiClient(client) .addInterceptor(loggingInterceptor) .addInterceptor(new StethoInterceptor()) .build(); }