shahbaz ansari
shahbaz ansari

Reputation: 35

Dagger + Retrofit dynamic baseUrl

I need to change the base url which can be entered by user that means the base URL can be totally different each time user enters it.

how could I achieve these.

What i have tried.

Retrofit2 Builder methods

retrofit.newBuilder().baseUrl("www.google.com").build()

But these is not overriding the base url which is provided while building the retrofit instance.

Tried @Url annotation but gives 405 Method not supported.

Upvotes: 0

Views: 2099

Answers (2)

M.ekici
M.ekici

Reputation: 763

After you have created an instance of retrofit you cannot change the base url. However, for each single request method in your interface you can change them dynamically. You can create a new instance of retrofit for each request but I don't suggest it because it is not efficient.

@POST()
suspend fun createRoom(
    @Url url: String = "www.....",
    @Body body: BodyDTO
): Response

in your case, you probably forget to add http method type annotation to your request method.

Upvotes: 2

Kunu
Kunu

Reputation: 5134

pass base url to your static method which will return Retrofit object

public static Retrofit getDynamicClient(String mBaseUrl) {
    retClient = new Retrofit.Builder().baseUrl(mBaseUrl).build();
    return retClient;
}

Upvotes: 0

Related Questions