Search code examples
androidretrofit2

Is it possible to use multiple baseurl in retrofit?


I want to use two server url using retrofit, but only one is working when I am using two base url. Please tell me how to use two base url in android.

public class APIUtils {
    public static String Url1 = "http://10.0.13.46:19460";
    public static String Url12 = "http://freshcamera.herokuapp.com";


    public static SOService getSOService(String url) {
        return RetrofitClient.getClient(url1).create(SOService.class);
    }

}

SOService class

public interface SOService {


   //URL 2
    @FormUrlEncoded
    @POST("/api/user/LoginUser")
    Call<Login> Login(@Field("username") String username, @Field("password")String password, @Field("grant_type")String passwords);
}

SOService_AI class

public interface SOService_AI {


    //URL 1
    @FormUrlEncoded
    @POST("/finalresult1")
    Call<List<AIImageProcessing>> AiImageCheck(@Field("img_data") String imgdata, @Field("name")String imgName);


}

Solution

  • 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();