public class OkHttp {
private static OkHttp instance;
private final OkHttpClient okHttpClient;
private OkHttp() {
okHttpClient = new OkHttpClient.Builder()
.writeTimeout(5000, TimeUnit.MILLISECONDS)
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(5000, TimeUnit.MILLISECONDS)
.addInterceptor(new HttpLoggingInterceptor())
.addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
if (message.startsWith("{") || message.startsWith("[")) {
Logger.json(message);
} else {
Logger.d(message);
}
}
}).setLevel(HttpLoggingInterceptor.Level.BODY)).build();
}
public static OkHttp getInstance() {
if (instance==null){
synchronized (OkHttp.class){
if (instance==null){
instance=new OkHttp();
}
}
}
return instance;
}
public OkHttpClient okHttpClient(){
return okHttpClient;
}
public class RetrofitManager {
private static RetrofitManager instance;
private final Retrofit retrofit;
private RetrofitManager() {
retrofit = new Retrofit.Builder().baseUrl(Api.Show_Data)
.client(OkHttp.getInstance().okHttpClient())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static RetrofitManager getInstance() {
if (instance==null){
synchronized (RetrofitManager.class){
if (instance==null){
instance=new RetrofitManager();
}
}
}
return instance;
}
public <T> T create(final Class<T> service) {
T t = retrofit.create(service);
return t;
}
public ApiService apiService(){
ApiService apiService = retrofit.create(ApiService.class);
return apiService;
}