Is it possible to use multiple baseurl in retrofit?

I guess what you need is changing URL at runtime to a completely different one.

For example, the following code will override the URL passed as baseUrl to retrofit object.

@GET
public Call<ResponseBody> profilePicture(@Url String url);

Note: You can't add url param to @GET and @POST. The URL must be passed to @Url.

// ERROR ( @Url cannot be used with @GET URL)
@GET("users") // or POST
public Call<Foo> getUsers(@Url String url);

// CORRECT
@GET
public Call<Foo> getUsers(@Url String fullUrl);

Checkout this tutorial for further information.


if you are working two url then you create two retrofit object. because single retrofit object work on single url. if you want to access two your make two retofit object like below code..

public class ApiClient {
private final static String BASE_URL = "https://simplifiedcoding.net/demos/";
private final static String BASE_URL2 = "http://freshcamera.herokuapp.com";

public static ApiClient apiClient;
private Retrofit retrofit = null;
private Retrofit retrofit2=null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}

public Retrofit getClient2() {
    return getClient2(null);
}


private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

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


    return retrofit;
}
private Retrofit getClient2(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

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


    return retrofit;
}

}

then after access like below code ..

        ApiClient.getInstance().getClient();
    ApiClient.getInstance().getClient2();