I am switching my Android app to use Retrofit2 instead Volley. Initially I had a singleton Retrofit instance which I would use to create the Retrofit service objects. But the app need to talk to services with different urls base urls. I am trying to figure out what is the best way to switch base urls in Retrofit. I have read about following solutions:
In my app 90% of the calls are made to same base url. The other 10% have like 4-5 different urls. Right now I feel like it is better to just use OkHttp to use these outliers calls.
Any ideas as to what is a good solution for this?
I've solved it, this way:
This is my retrofit instance:
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl("http://baseurl....")
.client(client)
.build()
and when I download data, I just change url this way:
@GET
fun downloadData(@Url url: String): Observable<Response<ResponseBody>>
why not create another API service, its work on me.
Class for API1
public class UtilsApi1 {
public static final String BASE_URL1 = "http://YourBaseURL1";
public static BaseApiService getAPI1(){
return RetrofitClient1.getClient(BASE_URL1).create(BaseApiService.class);
}
}
Class for API2
public class UtilsApi2 {
public static final String BASE_URL2 = "http://YourBaseURL2";
public static BaseApiService getAPI2(){
return RetrofitClient2.getClient(BASE_URL2).create(BaseApiService.class);
}
}
make 2 retrofit client, for example
public class RetrofitClient1 {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
return retrofit;
}
}
and [INTERFACE] for API Service
public interface BaseApiService {
@FormUrlEncoded
@POST("complete the URL")
Call<ResponseBody> get_methode (@Field("?") String ?);
}
you can call with this
BaseApiService Api1; //above the onCreate
inside onCreate
Api1 = UtilsApi1.get_methode();
public class RetrofitService {
public static String apiBaseUrl = "http://myurl";
private static Retrofit retrofit;
private static Retrofit.Builder builder =
new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiBaseUrl);
private static OkHttpClient.Builder httpClient =
new OkHttpClient.Builder();
public static void changeApiBaseUrl(String newApiUrl) {
apiBaseUrl = newApiUrl;
builder = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiBaseUrl);
}
public static <S> S createRetrofitService(Class<S> serviceClas) {
retrofit = builder.build();
return retrofit.create(serviceClass);;
}
your first API call would be
MyFirstApi api1=RetrofitService.createRetrofitService(MyFirstApi.class);
//..............
your second API call would be.
RetrofitService.changeApiBaseUrl("your new url");
MySecondApi api2=RetrofitService.createRetrofitService(MySecondApi.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With