用注解让 Retrofit 同时支持多个 baseUrl 以及动态改变 baseUrl。
- 支持多种用
@BaseUrl注解修改baseUrl的方式 - 支持用
globalBaseUrl修改全局的baseUrl - 优先使用
@Url修饰的全路径参数的baseUrl
在 settings.gradle 文件的 repositories 结尾处添加:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://www.jitpack.io' }
}
}或者在 settings.gradle.ktx 文件的 repositories 结尾处添加:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}添加依赖:
dependencies {
implementation("com.github.DylanCaiCoding:MultiBaseUrls:1.0.0")
}val okHttpClient = OkHttpClient.Builder()
.enableMultiBaseUrls()
// ...
.build()OkHttpClient okHttpClient = MultiBaseUrls.with(new OkHttpClient.Builder())
// ...
.build();使用 @BaseUrl 注解修改接口类里所有请求的 baseUrl。
@BaseUrl("https://xxxxxx.com/")
interface Api {
// ...
}@BaseUrl("https://xxxxxx.com/")
public interface Api {
// ...
}如果有运行时动态修改 baseUrl 的需求,可以修改全局的 globalBaseUrl,比如:
globalBaseUrl = "https://xxxxxx.com/"MultiBaseUrls.setGlobalBaseUrl("https://xxxxxx.com/");如果是有多个 baseUrl 需要在运行时动态修改,那就用 @BaseUrl 配置一个 key,用 dynamicBaseUrls[key] 动态修改 baseUrl。比如:
@BaseUrl(key = "url1")
interface Api {
@GET("/aaa/bbb")
@BaseUrl(key = "url2")
suspend fun request(): String
}
dynamicBaseUrls[url1] = "https://xxxxxx.com/v2/"
dynamicBaseUrls[url2] = "https://xxxxxx.com/v3/"@BaseUrl(key = "url1")
public interface Api {
@GET("/aaa/bbb")
@BaseUrl(key = "url2")
Single<String> request();
}
MultiBaseUrls.getDynamicBaseUrls().put("url1", "https://xxxxxx.com/v2/");
MultiBaseUrls.getDynamicBaseUrls().put("url2", "https://xxxxxx.com/v3/");遵循以下规则:
@Url注解的全路径参数优先级最高- 动态域名的优先级高于静态域名
- 函数注解的优先级高于类的注解
所以多个注解一起使用的情况按照下面的方式处理:
- 读取函数上的
@Url注解修饰的参数,如果参数传入的是全路径地址,那就直接使用该地址; - 读取函数上的
@BaseUrl注解,如果有配置key,并且dynamicBaseUrls里有对应的域名,那就使用该域名; - 读取类上的
@BaseUrl注解,如果有配置key,并且dynamicBaseUrls里有对应的域名,那就使用该域名; - 读取函数上的
@BaseUrl注解,如果有配置value为一个域名,那就使用该域名; - 读取类上的
@BaseUrl注解,如果有配置value为一个域名,那就使用该域名; - 读取
globalBaseUrl变量,如果有配置全局域名,那就使用该域名; - 使用
Retrofit创建时配置的baseUrl;
Copyright (C) 2024. Dylan Cai
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
