Square's Retrofit Android:Hash With Request of Contents

时间:2014-03-06 19:03:31

标签: android hash

我想使用Square的Retrofit库创建一个包含我请求的某些部分的哈希。 RequestInterceptor没有帮助我,因为它不提供有关请求的信息,它只是可以向它添加信息。 我需要访问HTTP动词,所有标头和REST路径来创建哈希。哈希将添加到Authorization标头中。 有什么想法吗?

1 个答案:

答案 0 :(得分:3)

为了使用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);
    }
}

. . .

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new YourInterceptor());
RestAdapter restAdapter = new RestAdapter.Builder()
    .setClient(new OkClient(okHttpClient))
    .build();