首页 > 解决方案 > Retrofit2 传递列表作为 URL 参数

问题描述

我正在尝试使用字符串列表发出 GET 请求。

像这样改造表示列表;

/endpoint?GTIN=111&GTIN=222

但是服务器(Springboot)只给出最后一个结果。

它适用于这个

/endpoint?GTIN=111,222

有没有办法在改造中做后者?

标签: retrofit2

解决方案


您可以尝试使用 Retrofit 的@Url注释来生成所需的 URL。

在您的服务接口文件中更改您的 API 方法,

public interface YourServiceInterface {

    @GET()  // DO NOT pass any arguments here
    Call<YourResponseObject> foo(@Url String url);  // use @Url annotation here

}

在您的活动或片段中。

String url = BASE_URL + "/endpoint?GTIN=111,222";   // https://example.com/endpoint?GTIN=111,222

YourServiceInterface api = ....;

Call<YourResponseObject> call = api.foo(url);

call.enqueue(/* implementation */);

推荐阅读