使用多个逗号分隔值进行改造查询(Q = 1,2,3,5,8)

时间:2018-01-21 23:39:30

标签: android api retrofit

@GET("?bs")
Observable<Response> getSomething(@Query("type") String first, String second, String third)

我希望最终结果如下:

api.com/endpoint/type=first,second,third

我如何做到这一点?

由于

3 个答案:

答案 0 :(得分:1)

我想出了一个想法

创建此类

 public class QueryParameters {

    private List<String> params = new ArrayList<>();


    public QueryParameters(String... params) {
        this.params.addAll(Arrays.asList(params));
    }

    public void add(String param) {
        params.add(param);

    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        int length = params.size();
        for (int i = 0; i < length; i++) {
            if (i == length - 1)
                builder.append(params.get(i));
            else
                builder.append(params.get(i)).append(",");
        }

        return builder.toString();
    }
}

创建QueryParameter的实例,然后使用构造函数或add()方法添加参数,然后调用toString()并将字符串传递给getSomething(@Query("commaSeparatedQuery") String commaSeparatedQuery);

答案 1 :(得分:1)

您可以使用单个String参数:

Observable<Response> getSomething(@Query("type") String type)

然后像这样调用它:getSomething("1,2,3,5,8")

答案 2 :(得分:0)

Kotlin与Rxjava示例

@GET("client/timeSlots")
fun getTimeSlot(
        @Query("size") size: Int = 14,
        @Query("page") page: Int = 1,
        @Query("cityId") cityId: Int,
        @Query("serviceIds",encoded = true) serviceIds: String?,
        ): Single<TimeSlotResponse>

函数以单个String返回int列表

    fun getServicesId(services: ArrayList<Int>): String? {
    var serviceName = ""
    services.forEach {
        serviceName += ",${it}"
    }
    serviceName = serviceName.removePrefix(",")
    return serviceName
}

输出将是这样

https://base_url/client/timeSlots?size=14&page=1&cityId=1&serviceIds=161,162,163,171