以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl.
RetrofitUrlManager
项目地址:JessYanCoding/RetrofitUrlManager
简介:以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl.
标签:
retrofit-
Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime.
Overview
Notice
Download
implementation 'me.jessyan:retrofit-url-manager:1.4.0'
Usage
Initialize
// When building OkHttpClient, the OkHttpClient.Builder() is passed to the with() method to initialize the configuration
OkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder())
.build();
Step 1
public interface ApiService {
@Headers({"Domain-Name: douban"}) // Add the Domain-Name header
@GET("/v2/book/{id}")
Observable<ResponseBody> getBook(@Path("id") int id);
}
Step 2
// You can change BaseUrl at any time while App is running (The interface that declared the Domain-Name header)
RetrofitUrlManager.getInstance().putDomain("douban", "https://api.douban.com");
If you want to change the global BaseUrl:
// BaseUrl configured in the Domain-Name header will override BaseUrl in the global setting
RetrofitUrlManager.getInstance().setGlobalDomain("your BaseUrl");