-
Notifications
You must be signed in to change notification settings - Fork 827
/
HttpModule.java
207 lines (184 loc) · 6.64 KB
/
HttpModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.codeest.geeknews.di.module;
import com.codeest.geeknews.BuildConfig;
import com.codeest.geeknews.app.Constants;
import com.codeest.geeknews.di.qualifier.GankUrl;
import com.codeest.geeknews.di.qualifier.GoldUrl;
import com.codeest.geeknews.di.qualifier.MyUrl;
import com.codeest.geeknews.di.qualifier.VtexUrl;
import com.codeest.geeknews.di.qualifier.WechatUrl;
import com.codeest.geeknews.di.qualifier.ZhihuUrl;
import com.codeest.geeknews.model.http.api.GankApis;
import com.codeest.geeknews.model.http.api.GoldApis;
import com.codeest.geeknews.model.http.api.MyApis;
import com.codeest.geeknews.model.http.api.VtexApis;
import com.codeest.geeknews.model.http.api.WeChatApis;
import com.codeest.geeknews.model.http.api.ZhihuApis;
import com.codeest.geeknews.util.SystemUtil;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by codeest on 2017/2/26.
*/
@Module
public class HttpModule {
@Singleton
@Provides
Retrofit.Builder provideRetrofitBuilder() {
return new Retrofit.Builder();
}
@Singleton
@Provides
OkHttpClient.Builder provideOkHttpBuilder() {
return new OkHttpClient.Builder();
}
@Singleton
@Provides
@ZhihuUrl
Retrofit provideZhihuRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, ZhihuApis.HOST);
}
@Singleton
@Provides
@WechatUrl
Retrofit provideWechatRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, WeChatApis.HOST);
}
@Singleton
@Provides
@GankUrl
Retrofit provideGankRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, GankApis.HOST);
}
@Singleton
@Provides
@GoldUrl
Retrofit provideGoldRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, GoldApis.HOST);
}
@Singleton
@Provides
@VtexUrl
Retrofit provideVtexRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, VtexApis.HOST);
}
@Singleton
@Provides
@MyUrl
Retrofit provideMyRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, MyApis.HOST);
}
@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();
}
@Singleton
@Provides
ZhihuApis provideZhihuService(@ZhihuUrl Retrofit retrofit) {
return retrofit.create(ZhihuApis.class);
}
@Singleton
@Provides
GankApis provideGankService(@GankUrl Retrofit retrofit) {
return retrofit.create(GankApis.class);
}
@Singleton
@Provides
WeChatApis provideWechatService(@WechatUrl Retrofit retrofit) {
return retrofit.create(WeChatApis.class);
}
@Singleton
@Provides
GoldApis provideGoldService(@GoldUrl Retrofit retrofit) {
return retrofit.create(GoldApis.class);
}
@Singleton
@Provides
VtexApis provideVtexService(@VtexUrl Retrofit retrofit) {
return retrofit.create(VtexApis.class);
}
@Singleton
@Provides
MyApis provideMyService(@MyUrl Retrofit retrofit) {
return retrofit.create(MyApis.class);
}
private Retrofit createRetrofit(Retrofit.Builder builder, OkHttpClient client, String url) {
return builder
.baseUrl(url)
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}