我想使用Square的Retrofit库创建一个包含我请求的某些部分的哈希.RequestInterceptor没有帮助我,因为它不提供有关请求的信息,它只是可以向它添加信息.我需要访问HTTP动词,所有标头和REST路径来创建哈希.哈希将添加到Authorization标头中.有任何想法吗?
为了使用 Retrofit 1.9.0 实现这一点,唯一的方法是使用 OkHttp 拦截器(https://github.com/square/okhttp/wiki/Interceptors)。以下代码使用 OkHttp 2.2.0:
public class YourInterceptor implements Interceptor {
@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (request != null) {
RequestBody body = request.body();
URL requestURL = request.url();
String method = request.method();
Headers headers = request.headers();
Request.Builder signedRequestBuilder = request.newBuilder();
signedRequestBuilder.addHeader("Authorization", "Your Signature");
request = signedRequestBuilder.build();
}
return chain.proceed(request);
}
}
Run Code Online (Sandbox Code Playgroud)
. . .
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new YourInterceptor());
RestAdapter restAdapter = new RestAdapter.Builder()
.setClient(new OkClient(okHttpClient))
.build();
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
779 次 |
| 最近记录: |