public abstract class RetrofitUtils {
private static RetrofitmRetrofit;
/**
* 獲取Retrofit對象
? ? * @return Retrofit
*/
? ? public static Retrofit getRetrofit() {
/**
* 設置攔截器
* 動態(tài)設置統(tǒng)一請求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;
}
}