Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Retrofit with different base urls

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:

  1. I have read threads where it is suggested switch base url at Interceptor level. This seems like a hacky solution, switching out base url at network layer.
  2. There is also the option of having multiple Retrofit instances to handle different urls. I don't quite like this as it may end up creating a lot of Retrofit instances.

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?

like image 270
araju Avatar asked Feb 03 '18 20:02

araju


3 Answers

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>>
like image 157
Palejandro Avatar answered Oct 07 '22 14:10

Palejandro


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();
like image 3
Ady Prasetyo Avatar answered Oct 07 '22 12:10

Ady Prasetyo


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); 
like image 1
Bishoy Kamel Avatar answered Oct 07 '22 12:10

Bishoy Kamel