因此,我正在尝试发出如下所示的 REST 请求:https://api.digitalocean.com/droplets/?client_id=[client_id]&api_key=[api_key]
端点在哪里https://api.digitalocean.com
,并且@GET("/droplets/")
将是注释。我希望自动添加结束位,因为它对于我发出的任何 API 请求都是相同的,并且将它添加到每个请求中会很麻烦。有没有办法做到这一点?
这是我的 Retrofit 2 拦截器:
private static class AuthInterceptor implements Interceptor {
private String mApiKey;
public AuthInterceptor(String apiKey) {
mApiKey = apiKey;
}
@Override
public Response intercept(Chain chain) throws IOException {
HttpUrl url = chain.request().httpUrl()
.newBuilder()
.addQueryParameter("api_key", mApiKey)
.build();
Request request = chain.request().newBuilder().url(url).build();
return chain.proceed(request);
}
}
RequestInterceptor
将实例传递给RestAdapter.Builder
添加查询参数的实例。
Retrofit 将为每个 API 调用调用请求拦截器,它允许您附加查询参数或替换路径元素。
在此回调中,您将能够为每个请求附加clientId
和查询参数。apiKey