Open FunnyLee opened 6 years ago
@Singleton @Provides OkHttpClient provideClient(OkHttpClient.Builder builder) { 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 (!SystemUtil.isNetworkConnected()) { request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } Response response = chain.proceed(request); if (SystemUtil.isNetworkConnected()) { 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; } }; // Interceptor apikey = new Interceptor() { // @Override // public Response intercept(Chain chain) throws IOException { // Request request = chain.request(); // request = request.newBuilder() // .addHeader("apikey",Constants.KEY_API) // .build(); // return chain.proceed(request); // } // } // 设置统一的请求头部参数 // builder.addInterceptor(apikey); //设置缓存 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(); }
private Retrofit createRetrofit(Retrofit.Builder builder, OkHttpClient client, String url) { return builder .baseUrl(url) .client(client) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); }
@Singleton @Provides OkHttpClient.Builder provideOkHttpBuilder() { return new OkHttpClient.Builder(); }
@Singleton @Provides Retrofit.Builder provideRetrofitBuilder() { return new Retrofit.Builder(); }
大神,你好。 在HttpModule类中,上面这几部分代码是不是可以写在一起?其实它的主要作用就是新建出一个Retrofit对象。所以我觉得是不是可以写在一起?
@Singleton @Provides OkHttpClient provideClient(OkHttpClient.Builder builder) { 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 (!SystemUtil.isNetworkConnected()) { request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } Response response = chain.proceed(request); if (SystemUtil.isNetworkConnected()) { 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; } }; // Interceptor apikey = new Interceptor() { // @Override // public Response intercept(Chain chain) throws IOException { // Request request = chain.request(); // request = request.newBuilder() // .addHeader("apikey",Constants.KEY_API) // .build(); // return chain.proceed(request); // } // } // 设置统一的请求头部参数 // builder.addInterceptor(apikey); //设置缓存 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(); }
private Retrofit createRetrofit(Retrofit.Builder builder, OkHttpClient client, String url) { return builder .baseUrl(url) .client(client) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); }
@Singleton @Provides OkHttpClient.Builder provideOkHttpBuilder() { return new OkHttpClient.Builder(); }
@Singleton @Provides Retrofit.Builder provideRetrofitBuilder() { return new Retrofit.Builder(); }
大神,你好。 在HttpModule类中,上面这几部分代码是不是可以写在一起?其实它的主要作用就是新建出一个Retrofit对象。所以我觉得是不是可以写在一起?