1

i'm using Retrofit, for my app, in the base interface, i'm using a base URL, like the retrofit page suggests, i want to be able to change the baseUrl from outside the interface, so to have one baseUrl for Testings, and one baseUrl for development, and that the user will be able to toggle between them. inside a java interface all variables are final and cannot be changed, and since the interface cannot take variables from outside of the enclosed interface, how do i change it in retrofit ?

here is my retrofit interface:

String baseUrl = "http://1.1.1.1:1000/";

    //network calls

    @GET("api/v1/alignment/measuring")
    Call<AligmentDataPojo> getCallData();

    @GET("api/v1/alignment/best-position")
    Call<BestPositionPojo> getBestPositionCallData();

    @GET("api/v1/data/resetBoxAligment")
    Call<AligmentScanPojo> resetBoxAligmentCallData();

    @GET("api/v1/alignment/pointer-location")
    Call<CurserLocationPojo> curserLocationCallData();

    @GET("api/v1/alignment/fineAlignment")
    Call<FineAligmentPojo> fineAligmentCallData();

    @GET("api/v1/alignment/get-bands")
    Call<GetBandsPojo> getAllBandsCallData();


    @POST("api/v1/alignment/set-band")
    Call<PojoSetBand> postAligmentSetBand(@Body SetBandAligmentModel setBandAligmentModel);

    @POST("api/v1/alignment/action/start")
    Call<PojoSetBand> postAligmentAction(@Body AligmentActionPayloadModel aligmentActionPayloadModel);


    @GET("api/v1/alignment/evaluation-results") //Just for mockuppurposeses we have unreal information that we send and simulate a return call
    Call<EvaluationDataPojo> getEvaluationResults();

    @POST("api/v1/alignment/start-evaluation") //Just for mockuppurposeses we have unreal information that we send and simulate a return call
    Call<PojoSetBand> startEvaluationPost(@Body AligmentActionPayloadModel aligmentActionPayloadModel);


    //testings
    @POST("api/v1/data/testPost")
    Call<TestPostModel> getTestPost(@Body TestPostModel testPostModel);


    //factory
    class Factory {
        private static RetrofitInterface service;

        public static RetrofitInterface getInstance() {
            if (service == null) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(baseUrl)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                service = retrofit.create(RetrofitInterface.class);
                return service;
            } else {
                return service;
            }

        }
    }
Chief Madog
  • 1,738
  • 4
  • 28
  • 55
  • 1/ Create a picker for the base URL 2/ On picking an URL save it to shared prefs or else... 3/ Restart app in order to re-instantiate Retrofit P.s. You can use compile 'com.jakewharton:process-phoenix:1.0.2' for the restart of the app. – loshkin Aug 18 '16 at 10:49
  • Maybe this is related. http://stackoverflow.com/questions/38805689/retrofit-change-baseurl – OneCricketeer Aug 18 '16 at 13:13
  • http://stackoverflow.com/questions/36960627/android-retrofit-design-patterns/36963162#36963162 take a look at this and create a setter for base URL. – Amir Aug 18 '16 at 13:29

2 Answers2

1

You can just create a different retrofit instance like this :

//Build retrofit instance
Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl).build();

Then you instanciate your service.

    apiService = retrofit.create(ApiService.class);
Mathieu Bertin
  • 1,634
  • 11
  • 11
0

The simple answer is just to use two RetroFit objects, since each object can only have one base url and they are designed to be used as singletons. Then when the user toggles between them, just use the RetroFit instance that corresponds to the selection.

Bryan
  • 14,756
  • 10
  • 70
  • 125