0

I have a hard-coded baseurl. I want to replace this hard-coded base url with the url returned from the web service I went to using the hard-coded url. I use retrofit 2 in my app.

return new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(new Utils.QualifiedTypeConverterFactory(
                            GsonConverterFactory.create(),
                            SimpleXmlConverterFactory.create()))
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(okHttpClient)
                    .build();

The Hard-coded BASE_URL looks like this:

public static String BASE_URL = "https://wstest.mesan.com";

And this is the url returning from the web service I called using this hard-coded url: "http://wsapi.mesan.com" Then changing baseurl will be used throughout the application.

How can i do this?

Any idea?

  • Instead of static You can use shared preference or local db to store your BASE_URL from web response and then you can replace your variable at run time – Nik Sep 07 '20 at 13:19
  • Use `@Path` for dynamic `url`. If you pass full url `retrofit` will replace your `base url`. – Bek Sep 07 '20 at 14:01
  • or `BASE_URL`(pass returned from api) as argument and create another api service or use `okhttpinterceptor` and change base url. There're multiple ways to achieve that. – Bek Sep 07 '20 at 14:11

0 Answers0