Cyrus the Great
Cyrus the Great

Reputation: 5942

Change retrofit baseurl that initialized in application class with dagger2

I created Application module called AppRetroServiceModule to provide retrofit.

@Module(includes = NetworkModule.class)
public class AppRetroServiceModule {

    @Provides
    @AppScope
    public RetroService retroService(Retrofit retrofit) {
        return retrofit.create(RetroService.class);
    }

    @Provides
    @AppScope
    public Retrofit retrofit(OkHttpClient okHttpClient, Gson gson) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(okHttpClient)
                .baseUrl("http://webservise/Srv1.svc/json/")
                .build();
    }
}

As you can see baseUrl is hardcoded and fixed. In application component I created getter to get retrofit and other provider, when application component is dependency for other component :

@AppScope
@Component(modules = {NetworkModule.class, AppRetroServiceModule.class})
public interface IApplicationComponent {
    RetroService getRetroService();

    Gson getGsonBuilder();

    Context getAppContext();
}  

I initialized dagger in application class:

public class App extends Application {
    private IApplicationComponent component;
    ...
    @Override
    public void onCreate() {
        super.onCreate();

        Timber.plant(new Timber.DebugTree());
        component = DaggerIApplicationComponent.builder()
                .networkModule(new NetworkModule(this))
                .build();

    }...

    public IApplicationComponent getAppComponent() {
        return component;
    }
}

Now in main activity i need to use other baseurl to fetching data from server :

public class MainActivity extends AppCompatActivity implements IMain.IMainView {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_m);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);

        DaggerIMainComponent.builder()
                .iApplicationComponent(((App) getApplication()).getAppComponent())
                .build()
                .inject(this);
    ...

Because retrofit is initialized when application is started, how can i change anytime retrofit baseurl when i need to fetch data from servers in other activity or fragment ? Sometimes i need to connect to number of servers and fetch data from these server and use in one activity then i need to change baseurl every i needed.

Upvotes: 1

Views: 746

Answers (1)

Pankaj Kumar
Pankaj Kumar

Reputation: 83018

If your base url is dynamic then you can use @Url. This will help you to pass dynamic base url to your requests.

public interface RetrofitClient {

    // Simple call with dynamic url
    @GET
    public Call<YouModelClass> doSomeRequest(@Url String url);

    // Call with dynamic url and request parameter
    @GET
    public Call<YouModelClass> doSomeRequest(@Url String url, @Query("id") String id);

    // Call with dynamic url and more request parameters
    @GET
    public Call<YouModelClass> doSomeRequest(@Url String url, @Query("id") String id, @Query("key") String key, @Query("part") String part);
}

And calling code would be

RetrofitClient service = retrofit.create(RetrofitClient.class);
Call<YouModelClass> call = service.doSomeRequest("your_dynamic_url_with_base_url");
// and so on

Read more at Retrofit 2 — How to Use Dynamic Urls for Requests

Upvotes: 1

Related Questions