我有一个新的安卓项目,我正在工作,Retrofit2正在为我工作。但是,我需要点击一个url,在我发送的数据之上使用两个字符串中的一个。
现在看起来是这样的:
@FormUrlEncoded
@POST("token")
Call<AccessResponse> requestAccess(@Field("grant_type") String type, @Field("code") String authCode, @Field("client_id") String ApiKey);授予类型只是两件事中的一件,我想将其抽象为静态urls,如下所示:
@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);有办法做到这一点吗?我两天的google-fu没有为我工作,也没有浏览API文档和代码。我只是不想在代码中的不同地方跟踪正确的字符串。
发布于 2016-02-03 15:03:21
发布于 2016-02-07 21:12:23
改造后的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"
}https://stackoverflow.com/questions/35093101
复制相似问题