Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit - Change BaseUrl

I have a scenario where I have to call an API with the same base URL, e.g. www.myAPI.com but with a different baseUrl.

I have an instance of Retrofit 2 which is built via a Builder:

return new Retrofit.Builder()     .baseUrl(FlavourConstants.BASE_URL)     .addConverterFactory(GsonConverterFactory.create(gson))     .client(okHttpClient)     .build(); 

The FlavourConstants.BASE_URL looks like this:

public static final String BASE_URL = "http://myApi.development:5000/api/v1/"; 

For some WebRequests, I must call the same API but on others, I must call it from a completely different BaseUrl. How do I change the Retrofit instance to therefore point to a different URL during runtime?

The Retrofit instance doesn't have a .setBaseUrl or setter or anything similar as it's built via a Builder.

Any ideas?

like image 959
Subby Avatar asked Aug 06 '16 15:08

Subby


1 Answers

Lucky for you Retrofit have a simple solution for that:

public interface UserManager {       @GET     public Call<ResponseBody> userName(@Url String url); } 

The url String should specify the full Url you wish to use.

like image 156
Nir Duan Avatar answered Oct 09 '22 13:10

Nir Duan