Any way to have generic URL using Retrofit

☆樱花仙子☆ 提交于 2020-01-11 11:24:08

问题


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);

    ...

}

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 ?


回答1:


See this other question

Apparently retrofit added an @Url annotation for that purpose.

public interface APIService {
    @GET
    Call<Users> getUsers(@Url String url);
}

This feature has been added in version 2.0.0.




回答2:


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

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.



来源:https://stackoverflow.com/questions/25400897/any-way-to-have-generic-url-using-retrofit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!