Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

android - Retrofit - Change BaseUrl

I have a scenario where I have to call an API with the same base URL, e.g. www.myAPI.com but with a different baseUrl.

I have an instance of Retrofit 2 which is built via a Builder:

return new Retrofit.Builder()
    .baseUrl(FlavourConstants.BASE_URL)
    .addConverterFactory(GsonConverterFactory.create(gson))
    .client(okHttpClient)
    .build();

The FlavourConstants.BASE_URL looks like this:

public static final String BASE_URL = "http://myApi.development:5000/api/v1/";

For some WebRequests, I must call the same API but on others, I must call it from a completely different BaseUrl. How do I change the Retrofit instance to therefore point to a different URL during runtime?

The Retrofit instance doesn't have a .setBaseUrl or setter or anything similar as it's built via a Builder.

Any ideas?

question from:https://stackoverflow.com/questions/38805689/retrofit-change-baseurl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Lucky for you Retrofit have a simple solution for that:

public interface UserManager {  
    @GET
    public Call<ResponseBody> userName(@Url String url);
}

The url String should specify the full Url you wish to use.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...