Home » Android » Android Retrofit – how to override baseUrl

Android Retrofit – how to override baseUrl

Posted by: admin May 14, 2020 Leave a comment

Questions:

In the retrofit adapter i have used a base Url for all my calls. so:

  Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

Lets say the above is my code, all my calls now use that baseUrl. But for one particular call, i want to change the base url, I dont want to create another rest adapter for this,as its just for testing locally. Can i change the end points in the interface possibly to not use the baseurl , or is there a annotation to supply my own base url ?

How to&Answers:

you can use the @Url annotation to provide the full complete url. E.G.

@GET
Call<GitHubUser> getUser(@Url String url);

Answer:

In retrofi2 the path in @GET overrides the base URL.

 @GET("https://someurl/api/supermarkets")
 Observable<List<TechIndex>> getTechIndexList();

The request will be send to “someurl/api/..” no matter what base url is.
Hope it helps