public class RetrofitManager {
private final String baseUrl;
private static RetrofitManager instance;
private static OkHttpClient okHttpClient;
//静态块
static {
getOkHttpClient();
}
private Retrofit retrofit;
private RetrofitManager(String baseUrl){
this.baseUrl = baseUrl;
initRetrofit();
}
public static synchronized RetrofitManager getInstance(String baseUrl){
if (instance==null){
synchronized (RetrofitManager.class){
if (null==instance){
instance = new RetrofitManager(baseUrl);
}
}
}
return instance;
}
public static OkHttpClient getOkHttpClient(){
if (okHttpClient==null){
synchronized (OkHttpClient.class){
if (null==okHttpClient){
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.connectTimeout(3000,TimeUnit.SECONDS)
.writeTimeout(3000,TimeUnit.SECONDS)
.readTimeout(3000,TimeUnit.SECONDS)
.build();
}
}
}
return okHttpClient;
}
private void initRetrofit(){
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public <T> T setCreate(Class<T> meq){
return retrofit.create(meq);
}
//retrofit2
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
//rxjava2
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
//gson的解析器
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
//logging-interceptor
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
//AndroidSchedulers
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'