If there exists multiple retrofit call, how can i make a singleton of a retrofit, so that there won't be repeated codes within the class, thereby get rid of unnecessary codes.
-
2I used dagger 2 for this. https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2 – Raghunandan Jan 16 '17 at 08:47
-
Creating a singleton for retrofit is no different than creating a normal singleton class. There are multiple ways of doing it which have been posted here and on google as well – ashkhn Jan 16 '17 at 08:48
5 Answers
Here's an example, but! Although this might be shiny and easy to use, singletons are evil. Try to avoid using them if possible. One way around it is by using dependency injection instead.
Anyway.
public class Api {
private static Api instance = null;
public static final String BASE_URL = "your_base_url";
// Keep your services here, build them in buildRetrofit method later
private UserService userService;
public static Api getInstance() {
if (instance == null) {
instance = new Api();
}
return instance;
}
// Build retrofit once when creating a single instance
private Api() {
// Implement a method to build your retrofit
buildRetrofit(BASE_URL);
}
private void buildRetrofit() {
Retrofit retrofit = ...
// Build your services once
this.userService = retrofit.create(UserService.class);
...
}
public UserService getUserService() {
return this.userService;
}
...
}
Now you have everything in one place. Use it.
UserService userService = Api.getInstance().getUserService();

- 2,216
- 10
- 19
-
1As you have said, they are evil. I stumbled across a problem few weeks ago. When using the same API for different backends (so the domain changes in runtime) then the singleton does not create new instances from the factory. Using a factory pattern like it is in koin solved it. But now I have thousends of api instances lieing around. Any ideas to improve this? – kaulex Jun 03 '20 at 17:03
To implement the singleton class, the simplest way is to make the constructor of the class as private.
- Eager initialization:
In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class.
public class SingletonClass {
private static volatile SingletonClass sSoleInstance = new SingletonClass();
//private constructor.
private SingletonClass(){}
public static SingletonClass getInstance() {
return sSoleInstance;
}
}
- Lazy initialization:
This method will check if there is any instance of that class is already created? If yes, then our method (getInstance()) will return that old instance and if not then it creates a new instance of the singleton class in JVM and returns that instance. This approach is called as Lazy initialization.
public class SingletonClass {
private static SingletonClass sSoleInstance;
private SingletonClass(){} //private constructor.
public static SingletonClass getInstance(){
if (sSoleInstance == null){ //if there is no instance available... create new one
sSoleInstance = new SingletonClass();
}
return sSoleInstance;
}
}
There are few more things like Java Reflection API, Thread Safe & Serialization safe Singleton.
Please check this reference for more details and in-depth understanding of singleton object creation.
https://medium.com/@kevalpatel2106/digesting-singleton-design-pattern-in-java-5d434f4f322#.6gzisae2u

- 1,734
- 1
- 14
- 34
public class Singleton {
private volatile static Singleton singleton;
private Singleton(){}
public static Singleton getSingleton(){
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}

- 11
- 5
public class Singleton {
private static Singleton INSTANCE = null;
// other instance variables can be here
private Singleton() {};
public static Singleton getInstance() {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
return(INSTANCE);
}
// other instance methods can follow
}
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
@Module
public class NetworkModule {
@Provides
@Singleton
public Gson gson() {
GsonBuilder gsonBuilder = new GsonBuilder();
return gsonBuilder.create();
}
@Provides
@Singleton
public HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(
message -> Timber.i(message));
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}
@Provides
@Singleton
public Cache cache(File cacheFile) {
return new Cache(cacheFile, 10 * 1000 * 1000); //10MB Cache
}
@Provides
@Singleton
public File cacheFile(@ApplicationContext Context context) {
return new File(context.getCacheDir(), "okhttp_cache");
}
@Provides
@Singleton
public OkHttpClient okHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache) {
return new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.cache(cache)
.build();
}
@Provides
@Singleton
public Retrofit retrofit(OkHttpClient okHttpClient, Gson gson) {
return new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.baseUrl("you/base/url")
.build();
}
}

- 5
- 1
-
Singleton best and simple example of here ... http://thientvse.com/2017/10/android-design-patterns-singleton-pattern/ – hardy thummar Aug 30 '19 at 09:44
i have tried in kotlin :
class RetrofitClient{
companion object
{
var retrofit:Retrofit?=null;
fun getRetrofitObject():Retrofit?
{
if(retrofit==null)
{
synchronized(RetrofitClient ::class.java)
{
retrofit=Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("YOUR_BASE_URL")
.build()
}
}
return retrofit
}
}
}
and then:
var service:ServicesInterface?= RetrofitClient.getRetrofitObject()?.create(ServicesInterface::class.java)

- 1,177
- 12
- 16