How to make the creation of a retrofit in a separate class?
So that I can initialize it in different places?
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); } }
Source: https://ru.stackoverflow.com/questions/590301/
All Articles
его в разных местах инициализировать? 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