Retrofit获取当前的HTTP请求方法

时间:2015-09-18 14:38:46

标签: java twitter retrofit

我正在使用RequestInterceptor来授权对twitter rest apis的请求 创建此处描述的签名时:https://dev.twitter.com/oauth/overview/creating-signatures
我需要获取当前的HTTP方法GET或POST 我该怎么办?
感谢

1 个答案:

答案 0 :(得分:0)

使用OkHttp的Interceptor代替改造中的RequestInterceptor

class SigningInterceptor implements Interceptor {
  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    final String method = request.method();

    // Build your sig and use it to build an auth header

    Request newRequest = request.newBuilder()
        .header("Authorization", /* the computed auth header */)
        .builder();

    return chain.proceed(newRequest);
  }
}

并添加到您的OkHttpClient -

OkHttpClient client = new OkHttpClient();
client.interceptors().add(new SigningInterceptor());

并在构建改造实例时使用client