Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

关于HttpModule类中,构建Retrofit对象的问题 #139

Open
FunnyLee opened this issue Oct 19, 2018 · 0 comments
Open

关于HttpModule类中,构建Retrofit对象的问题 #139

FunnyLee opened this issue Oct 19, 2018 · 0 comments

Comments

@FunnyLee
Copy link

FunnyLee commented Oct 19, 2018

@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对象。所以我觉得是不是可以写在一起?

@FunnyLee FunnyLee changed the title 关于AppModule类中,构建Retrofit对象的问题 关于HttpModule类中,构建Retrofit对象的问题 Oct 19, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant