为什么我不能在Retrofit上添加标题到拦截器requeset OkHttp?

时间:2017-03-29 14:11:15

标签: android rest retrofit interceptor

你能否在代码中指出我的错误,我无法向拦截器添加标题“授权”。当我发出请求时,它会抛出错误 retrofit2.adapter.rxjava.HttpException:HTTP 401 Unauthorized 。但是当我为每个请求添加标头令牌时

@Header("Authorization") String token

然后它有效。

这是我的代码:

 protected Retrofit retrofit;
protected OkHttpClient client;
protected T api;
protected UserCredential userCredential;

public RestClient(Class<T> type) {
    if (client == null) {
        synchronized (OkHttpClient.class) {
            client = new OkHttpClient();
            client.newBuilder().addInterceptor(chain -> {
                Request original = chain.request();
                Request.Builder builder = original.newBuilder();
                if (GrabDoiApplication.getInstance().getCurrentCredentials() != null) {
                    String token = GrabDoiApplication.getInstance().getCurrentCredentials().getToken();
                    if (token != null) {
                        builder.addHeader("Authorization", token);
                    }
                }
                builder.method(original.method(), original.body());

                return chain.proceed(builder.build());
            }).build();

            if (retrofit == null) {
                synchronized (Retrofit.class) {
                    Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                            .baseUrl(Constants.GRABDOI_SERVER_URL)
                            .client(client)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJavaCallAdapterFactory.create());
                    retrofit = retrofitBuilder.build();
                }
            }
        }
    }
    api = retrofit.create(type);
}

1 个答案:

答案 0 :(得分:2)

您可以尝试将对静态方法的访问权限放在拦截器的主体之外:

    public RestClient(Class<T> type) {
    if (retrofit != null) {
        return;
    }
    synchronized (OkHttpClient.class) {
        client = new OkHttpClient();
        OkHttpClient.Builder builder = client.newBuilder();
        final String token;
        if (GrabDoiApplication.getInstance().getCurrentCredentials() != null &&
                (token = GrabDoiApplication.getInstance().getCurrentCredentials().getToken()) != null) {
            builder.addInterceptor(chain -> {
                Request.Builder builder = chain.request().newBuilder();
                builder.addHeader("Authorization", token);
                builder.method(original.method(), original.body());
                return chain.proceed(builder.build());
            });
        }
        client = builder.build();

        synchronized (Retrofit.class) {
            Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
                    .baseUrl(Constants.GRABDOI_SERVER_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create());
            retrofit = retrofitBuilder.build();
        }
    }


    api = retrofit.create(type);
}