1

Here is my singleton class.

public class GetRetrofit {


static volatile Retrofit retrofit = null;

public static Retrofit getInstance() {
    if (retrofit == null) {
        synchronized (GetRetrofit.class) {
            if (retrofit == null) {
                OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
                builder.readTimeout(30, TimeUnit.SECONDS);
                builder.connectTimeout(30, TimeUnit.SECONDS);

                HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                builder.addInterceptor(interceptor);

                //  builder.addInterceptor(new UnauthorisedInterceptor(context));
                OkHttpClient client = builder.build();

                retrofit =
                        new Retrofit.Builder().baseUrl("DYNAMIC_URL")
                                .client(client).addConverterFactory(GsonConverterFactory.create()).build();

                //addConverterFactory(SimpleXmlConverterFactory.create())
            }
        }
    }

    return retrofit;

}
}

I want to change dynamic base url.

e.g : http://192.168.1.60:8888/property/Apiv1 need to change this url in run time http://192.168.1.50:8008/inventory/Apiv1.

How can i change this two url dynamically in runtime. Please help me.

Murali Ganesan
  • 2,925
  • 4
  • 20
  • 31
  • Please check this answer: https://stackoverflow.com/questions/32559333/retrofit-2-dynamic-url – Lilia Jan 02 '20 at 12:54

1 Answers1

2

Following code will show one way of changing base url of retrofit at run time but at the expense of slightly breaking the singleton pattern. The pattern still partially applicable though. I'll explain that later on.

Check this modified version of GetRetrofit out.

public class GetRetrofit {

      static volatile Retrofit retrofit = null;
      private static String baseUrlString = "http://192.168.1.60:8888/property/Apiv1";

      public static void updateBaseUrl(String url){
              baseUrlString = url;

              retrofit = getRetrofitObj();
      }

      public static Retrofit getInstance() {
              if (retrofit == null) {
                      synchronized (GetRetrofit.class ) {
                              if (retrofit == null) {
                                      retrofit = getRetrofitObj();
                              }
                      }
              }

              return retrofit;
      }

      public static Retrofit getRetrofitObj() {
              OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
              builder.readTimeout(30, TimeUnit.SECONDS);
              builder.connectTimeout(30, TimeUnit.SECONDS);

              HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
              interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
              builder.addInterceptor(interceptor);

              //  builder.addInterceptor(new UnauthorisedInterceptor(context));
              OkHttpClient client = builder.build();

              retrofit = new Retrofit.Builder().baseUrl(baseUrlString)
                         .client(client).addConverterFactory(GsonConverterFactory.create()).build();
      }
}

So, GetRetrofit has this baseUrlString that initializes/holds our dynamic url. If you don't need to update the default base url then you're fine calling,

Retrofit retrofit = GetRetrofit.getInstance();

But When we need to change the default url to something else (http://192.168.1.50:8008/inventory/Apiv1, for instance), we need to update our base url first.

GetRetrofit.updateBaseUrl("http://192.168.1.50:8008/inventory/Apiv1");
Retrofit retrofit = GetRetrofit.getInstance(); // this provides new retrofit instance with given url

This builds a new retrofit instance based on this new url only if you update the url. As long as you don't need to update the base url, you can get the singleton retrofit instance simply by calling,

Retrofit retrofit = GetRetrofit.getInstance();

So in a way it partially retains the trait of singleton pattern.

Hope this helps.

fluffyBatman
  • 6,524
  • 3
  • 24
  • 25