可以在Android中支持同一API的不同版本

时间:2019-02-21 20:50:12

标签: android api retrofit version

我有一个Android应用,正在使用Retrofit发出http请求。服务器端提出了该API的更新版本。我的应用程序必须同时支持旧版本和新版本。例如:

  1. 旧版本:https://api.company.com/abc/def/2018-01-01/add
  2. 新版本:https://api.company.com/abc/def/2019-01-01/add

我的BASE_URL是:https://api.company.com

我的问题是,在Retrofit中同时支持两个版本的最佳方法是什么。在运行时决定使用旧版本还是新版本。

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用路径和界面执行类似的操作

@GET("abc/def/{version}/add")
    Call<List<String>> getFilterValues(@Path(value = "version", encoded = true))
然后,

版本会放入您拥有{version}的网址中

用法示例

String version = "2018-01-01";

if(useNewVersion){
    version = "2019-01-01";
}

Call<List<String>> call = api.getFilterValues(version);

答案 1 :(得分:0)

以下是上述答案的扩展变体,但是如果端点太多,也将很困难:

@GET
Single<JsonResponse> getSomething(@Url String url);


String requestUrl = "....";
restApiService.getSomething(requestUrl);