Square's Retrofit Android:Hash With Request of Contents

Mar*_*tes 5 hash android

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

Mar*_*tes 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);
    }
}
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)