How to make the creation of a retrofit in a separate class?

So that I can initialize it in different places?

  • Are you sure that you need его в разных местах инициализировать ? What do you mean by initialization? Maybe you just want to use the same Retrofit instance in different parts of the program? If so, why? - post_zeew
  • @post_zeew, yes, I want to use the same Retrofit instance in different parts of the program? - Martinez Toni

2 answers 2

Create a singleton in the application for example. Or you can use dagger2

    public class PhpClient {

     private static IPhpApi API; public static IPhpApi getApi() { if (API == null) setupClient(); return API; } private static void setupClient() { OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new HttpLoggerInterceptor()).build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(Config.BASE_SCRIPT_URL) .client(okHttpClient) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); API = retrofit.create(IPhpApi.class); } 

    }