Retrofit: Allowing base url to null until dynamically change it later

Created on 20 Jan 2017  路  2Comments  路  Source: square/retrofit

What kind of issue is this?

  • [ ] Feature Request. Start by telling us what problem you鈥檙e trying to solve. Often a solution
    already exists! Don鈥檛 send pull requests to implement new features without first getting our
    support. Sometimes we leave features out on purpose to keep the project small.

I am building a serverless app which does not know the base url until the users set it up. However, I am also using Dagger 2 to initialize the Retrofit RestAPI adapter when the application gets started before a user has an opportunity to configure the server base url. But Retrofit2 does not allow empty base url, which makes my app crash during the initialization of Dagger2. So far, I could resolve it by providing a dummy but valid http url and change it base url after the user configure the correct one. But this does not feel right. I am wondering whether there is a better solution or not?

Any suggestion will be appreciated.

Most helpful comment

You can either defer initialization of the Retrofit instance until the URL is known, or you can use a placeholder like http://localhost/ and use an OkHttp interceptor to rewrite all URLs with this host to whatever value is configured.

All 2 comments

You can either defer initialization of the Retrofit instance until the URL is known, or you can use a placeholder like http://localhost/ and use an OkHttp interceptor to rewrite all URLs with this host to whatever value is configured.

Thank you very much for the confirmation. I will try to use the delay initialization way.

Was this page helpful?
0 / 5 - 0 ratings