Android Retrofit通过OkHttp添加Interceptor拦截器设置Get请求公共参数

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
pubic Retrofit getRetrofit() {
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if ("GET".equals(request.method())) {
HttpUrl httpUrl = request.url().newBuilder()
.addQueryParameter("version", "1.0.0")
.addQueryParameter("appkey","yeyuanxinyi")
.addQueryParameter("timestamp", String.valueOf(System.currentTimeMillis()))
.build();
request = request.newBuilder().url(httpUrl).build();
}
return chain.proceed(request);
}
})
.build();
return new Retrofit.Builder()
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(getBaseUrl())
.build();
}

Click and drag to move