Retrofit 能否支持 Android 中相同 api 的不同版本
Can Retrofit support different versions of the same api in Android
我有一个 Android 应用程序,我正在使用 Retrofit 发出 http 请求。服务器端已经拿出了api的更新版本。我的应用程序必须同时支持旧版本和新版本。例如:
- 旧版本:
https://api.company.com/abc/def/2018-01-01/add
- 新版本:
https://api.company.com/abc/def/2019-01-01/add
我的 BASE_URL 是:https://api.company.com
.
我的问题是,在 Retrofit 中支持这两个版本的最佳方式是什么。使用旧版本还是新版本的决定是在运行时做出的。
提前致谢。
您可以对路径和界面执行类似的操作
@GET("abc/def/{version}/add")
Call<List<String>> getFilterValues(@Path(value = "version", encoded = true))
版本将被放入 url 中,您有 {version}
使用示例
String version = "2018-01-01";
if(useNewVersion){
version = "2019-01-01";
}
Call<List<String>> call = api.getFilterValues(version);
这是上述答案的扩展变体,但如果端点太多,我也会感到困难:
@GET
Single<JsonResponse> getSomething(@Url String url);
String requestUrl = "....";
restApiService.getSomething(requestUrl);
我有一个 Android 应用程序,我正在使用 Retrofit 发出 http 请求。服务器端已经拿出了api的更新版本。我的应用程序必须同时支持旧版本和新版本。例如:
- 旧版本:
https://api.company.com/abc/def/2018-01-01/add
- 新版本:
https://api.company.com/abc/def/2019-01-01/add
我的 BASE_URL 是:https://api.company.com
.
我的问题是,在 Retrofit 中支持这两个版本的最佳方式是什么。使用旧版本还是新版本的决定是在运行时做出的。
提前致谢。
您可以对路径和界面执行类似的操作
@GET("abc/def/{version}/add")
Call<List<String>> getFilterValues(@Path(value = "version", encoded = true))
版本将被放入 url 中,您有 {version}
使用示例
String version = "2018-01-01";
if(useNewVersion){
version = "2019-01-01";
}
Call<List<String>> call = api.getFilterValues(version);
这是上述答案的扩展变体,但如果端点太多,我也会感到困难:
@GET
Single<JsonResponse> getSomething(@Url String url);
String requestUrl = "....";
restApiService.getSomething(requestUrl);