向API请求动态添加可选参数

时间:2021-09-29 10:56:54

I have an Android app that communicates with a REST API.

我有一个与REST API通信的Android应用程序。

For each request, I want my app to be able to add optional parameters in addition to the mandatory parameters.

对于每个请求,我希望我的应用程序除了必需参数外还能够添加可选参数。

How can I implement this with Retrofit? Currently all the parameters are hard-coded in the interface:

如何使用Retrofit实现此功能?目前,所有参数都在接口中进行了硬编码:

@GET("/user/{id}/comments?position={pos}")  
void getComments(@Path("id") int id, @Query("pos") int pos, Callback<String> cb);

@GET("/user/{id}/likes?n={number}")  
void getLikes(@Path("id") int id, @Query("number") int number, Callback<String> cb);

/* etc */

Is it possible to "sub-class" the RestAdapter or something to be able to dynamically add optional parameters to my requests?

是否可以对RestAdapter进行“子类化”,或者能够为我的请求动态添加可选参数?

2 个解决方案

#1


44  

You have a few ways to achieve that:

您有几种方法可以实现这一目标:

  • By default Retrofit handles nulls correctly for all null query parameters, so you can do something like:

    默认情况下,Retrofit会为所有空查询参数正确处理空值,因此您可以执行以下操作:

    @GET("/user/{id}/likes")  
    void getLikes(@Path("id") int id, @Query("n") Integer number, @Query("pos") Integer pos Callback<String> cb);
    

If you use Object instead of int you can call to the method using null for the optional parameters:

如果使用Object而不是int,则可以使用null为可选参数调用方法:

    getLikes(1, null, null, cb); // to get /user/1/likes
    getLikes(1, 2, null, cb); // to get /user/1/likes?n=2
  • By using RequestInterceptor:

    通过使用RequestInterceptor:

    RestAdapter.Builder builder= new RestAdapter.Builder()
    .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("Accept", "application/json;versions=1");
                    if(/*condition*/){
                       request.addQueryParam(arg0, arg1)
                    }                      
                }
            });
    

#2


16  

Support for Map<String,String> is now available. Just use @QueryMap Map<String, String> params.

现在可以使用对Map 的支持。只需使用@QueryMap Map params。 ,string> ,string>

From http://square.github.io/retrofit/:

For complex query parameter combinations a Map can be used.

对于复杂的查询参数组合,可以使用Map。

Example:

@GET("/group/{id}/users")
List<User> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);

#1


44  

You have a few ways to achieve that:

您有几种方法可以实现这一目标:

  • By default Retrofit handles nulls correctly for all null query parameters, so you can do something like:

    默认情况下,Retrofit会为所有空查询参数正确处理空值,因此您可以执行以下操作:

    @GET("/user/{id}/likes")  
    void getLikes(@Path("id") int id, @Query("n") Integer number, @Query("pos") Integer pos Callback<String> cb);
    

If you use Object instead of int you can call to the method using null for the optional parameters:

如果使用Object而不是int,则可以使用null为可选参数调用方法:

    getLikes(1, null, null, cb); // to get /user/1/likes
    getLikes(1, 2, null, cb); // to get /user/1/likes?n=2
  • By using RequestInterceptor:

    通过使用RequestInterceptor:

    RestAdapter.Builder builder= new RestAdapter.Builder()
    .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("Accept", "application/json;versions=1");
                    if(/*condition*/){
                       request.addQueryParam(arg0, arg1)
                    }                      
                }
            });
    

#2


16  

Support for Map<String,String> is now available. Just use @QueryMap Map<String, String> params.

现在可以使用对Map 的支持。只需使用@QueryMap Map params。 ,string> ,string>

From http://square.github.io/retrofit/:

For complex query parameter combinations a Map can be used.

对于复杂的查询参数组合,可以使用Map。

Example:

@GET("/group/{id}/users")
List<User> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);