Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the baseUrl at runtime. #1404

Closed
julioromano opened this Issue Dec 23, 2015 · 7 comments

Comments

7 participants
@julioromano
Copy link

julioromano commented Dec 23, 2015

I have a use case in which the baseUrl of the application depdends on a user setting that can be changed at runtime.

I build retrofit with:

Retrofit retrofit = Retrofit.Builder().baseUrl(baseUrl).build();
MyApi myApi = retrofit.create(MyApi.class);

Is it possible to change the baseUrl setting after this initialization without having to rebuild (and thus repropagate to all my objects) all instances of the retrofit objects?

Thanks,
Bye,
Marco

@JakeWharton

This comment has been minimized.

Copy link
Collaborator

JakeWharton commented Dec 23, 2015

Implement BaseUrl and pass that instead of a fixed URL. It will be asked for the URL for each request, allowing it to change over time.

@maoruibin

This comment has been minimized.

Copy link

maoruibin commented Jan 22, 2016

thanks J.W, it's very useful ~ ^_^

@Zhuinden

This comment has been minimized.

Copy link

Zhuinden commented Apr 8, 2016

Has it been changed to HttpUrl?

@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Apr 8, 2016

Unfortunately I removed this because it’s not safe.
#1652

You can workaround with this:
https://gist.github.com/swankjesse/8571a8207a5815cca1fb

@gobera

This comment has been minimized.

Copy link

gobera commented Aug 2, 2016

@swankjesse Your link https://gist.github.com/swankjesse/8571a8207a5815cca1fb just contains the class HostSelectionInterceptor.java. How do i wire it up with Dagger 2, Retrofit, okHttp and Gson to make API calls to 3 different urls, with each call done in a class of it's own where i have injected a Retrofit dependecy? Please, just began using Dagger2.

@swankjesse

This comment has been minimized.

Copy link
Member

swankjesse commented Aug 2, 2016

I think you might make your HostSelectionInterceptor a singleton (@Singleton) and use it as an argument to your@Provides method when you build your OkHttpClient:

  @Provides public OkHttpClient provideOkHttpClient(
      HostSelectionInterceptor hostSelectionInterceptor) {
    return new OkHttpClient.Builder()
        .addInterceptor(hostSelectionInterceptor)
        .build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.