public class RetrofitUtils {
private static RetrofitUtils instance;
private RetrofitUtils(){}
public static RetrofitUtils getInstance(){
if(instance==null){
synchronized (RetrofitUtils.class){
if(instance==null){
instance=new RetrofitUtils();
}
}
}
return instance;
}
private static Retrofit retrofit;
public static Retrofit getRetrofit(String url){
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(getOkHttpClient())
.build();
return RetrofitUtils.retrofit;
}
private static OkHttpClient okHttpClient;
private static synchronized OkHttpClient getOkHttpClient(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient=new OkHttpClient.Builder()
.connectTimeout(20,TimeUnit.SECONDS)
.writeTimeout(20,TimeUnit.SECONDS)
.readTimeout(20,TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build();
return okHttpClient;
}
public <T> T doPost(String url,Class<T> apiService){
Retrofit retrofit = getRetrofit(url);
T t = retrofit.create(apiService);
return t;
}
}