public abstract class RetrofitUtils {
private static RetrofitmRetrofit;
/**
* 获取Retrofit对象
* @return Retrofit
*/
public static Retrofit getRetrofit() {
/**
* 设置拦截器
* 动态设置统一请求Header
*/
OkHttpClient httpClient=new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain)throws IOException {
Request request = chain.request().newBuilder()
.addHeader("deviceId",Constants.deviceId)
.build();
return chain.proceed(request);
}
})
.build();
if (null ==mRetrofit) {
mRetrofit =new Retrofit.Builder()
.client(httpClient)
.baseUrl(Constants.BASE_URL+"/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
return mRetrofit;
}
}