问题
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