I usually created a Retrofit client in the App class in onCreate (), but I heard the opinion that this is wrong, because in fact it turns out that this is not Singleton. Where and how best to create a Retrofit client?
My sample code is:
public class App extends Application { public static MessengerApi service; private static OkHttpClient client; private static Retrofit retrofit; @Override public void onCreate() { super.onCreate(); HeaderInterceptor headerInterceptor = new HeaderInterceptor(); HttpLoggingInterceptor logger = new HttpLoggingInterceptor(); logger.setLevel(HttpLoggingInterceptor.Level.BODY); client = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .addInterceptor(logger) .addInterceptor(headerInterceptor) .build(); retrofit = new Retrofit.Builder() .baseUrl(BuildConfig.BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); service = retrofit.create(MessengerApi.class); } }