以任何方式使用改造有通用网址 [英] Any way to have generic URL using Retrofit

查看:126
本文介绍了以任何方式使用改造有通用网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有接受相同的GET参数(主要为分页目的)几个网址如下:

I have several URL that accepts the same GET parameters (mainly for pagination purposes) as follow :

public interface AsynchronousApi {

    @GET("/api/users")
    public void listUsers(@Query("limit") Integer limit,
                          @Query("offset") Integer offset,
                         Callback<userList> callback);

    @GET("/api/posts")
    public void listPosts(@Query("limit") Integer limit,
                          @Query("offset") Integer offset,
                          Callback<postList> callback);

    ...

}

因为我有很多的URL,这是有点重复。
所以,我想有办法来重构这个这样我就不必重复@Query(限制)和@Query(抵消)每次。也许另一个注释将帮助?

Since I have lots of URL, this is getting a bit repetitive. So I would like to have way to refactor this so I don't have to repeat @Query("limit") and @Query("offset") everytime. Maybe another annotation would help ?

推荐答案

没有。改造价值观不同的方法比接口方法声明的重复数据删除更语义权重。

No. Retrofit values the semantic weight of separate methods much more than de-duplication of interface method declarations.

虽然API端点的类似行为是好的API设计,这将是Java的设计很差。如果此服务被本地(即你的应用程序内),因为它们取出两个完全不同的事情,你就不会巩固了两种方法。

While the similar behavior of the API endpoints is good API design, it would be poor Java design. If this service was local (i.e., inside your app) you wouldn't consolidate the two methods because they fetch two very different things.

这篇关于以任何方式使用改造有通用网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆