Retrofit2静态编码表单数据?

时间:2016-01-29 20:04:33

标签: android retrofit2

我正在开发一个新的Android项目,而Retrofit2对我来说效果很好。但是,我需要在我发送的数据之上使用两个字符串中的一个来命中一个网址。

现在它看起来像这样:

@FormUrlEncoded
@POST("token")
Call<AccessResponse> requestAccess(@Field("grant_type") String type, @Field("code") String authCode, @Field("client_id") String ApiKey);

授权类型只是两件事之一,我想把它抽象成静态网址,如下所示:

@FormUrlEncoded
@POST("token")
@Field("grant_type","type1")
Call<AccessResponse> requestAccess( @Field("code") String authCode, @Field("client_id") String ApiKey);

@FormUrlEncoded
@POST("token")
@Field("grant_type","type2")
Call<AccessResponse> refreshAccess( @Field("code") String authCode, @Field("client_id") String ApiKey);

有没有办法实现这个目标?我的2天google-fu避风港对我有用,也没有浏览API文档和代码。我只是不想在我的代码中的各个地方跟踪正确的字符串。

2 个答案:

答案 0 :(得分:0)

原来答案是“你现在不能”。

功能请求<{3}}

还有另一种方法,一个带有open issue on Github中提到的方法示例的FieldMap对象,但它并不是我想要的,而且仅仅是一个字段的过度杀伤。

答案 1 :(得分:0)

Retrofit RequestInterceptor可以完成这项工作吗? 它可以为每个请求注入参数......所以从那里你可以编写一个方法,根据你想要做的事情注入正确的参数......

RequestInterceptor requestInterceptor = new RequestInterceptor() {
       @Override
       public void intercept(RequestFacade request) {
           request.addQueryParam("grant_type", getGrantType());
       }
};
private String getGrantType()
{
     // do your stuff and :   
    return "type1"; // or "type2"  
}
相关问题