Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Retrofit - how to override baseUrl

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 ?

like image 210
j2emanue Avatar asked Jan 20 '16 18:01

j2emanue


People also ask

How do I change my retrofit URL on Android?

For latest Retrofit library, you can simply use singleton instance and change it with retrofitInstance. newBuilder(). baseUrl(newUrl) . No need to create another instance.

How do I change my API base URL?

First, remove your prefix: api/v1/ as it only sets-up a global prefix, which is not what you want. Method 1 - via php: Add #[ApiResource(routePrefix: '/v1')] in your Book entity class: #[ApiResource(routePrefix: '/v1')] class Book { //... }

What is retrofit Library in Android?

Retrofit is a type-safe REST client for Android, Java and Kotlin developed by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. See this guide to understand how OkHttp works.


2 Answers

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

@GET
Call<GitHubUser> getUser(@Url String url);
like image 182
Blackbelt Avatar answered Oct 18 '22 20:10

Blackbelt


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

like image 42
Zhangali Bidaibekov Avatar answered Oct 18 '22 20:10

Zhangali Bidaibekov