从现有的OkHttpClient添加拦截器

时间:2016-11-21 14:32:42

标签: android retrofit okhttp

我已经构建了OkHttpClient的实例。如何添加拦截器? (我也在使用Retrofit)

我希望将授权令牌添加到其中。我目前收到以下错误:

Error creating evaluation class loader: com.intellij.debugger.engine.evaluation.EvaluateException: Method threw 'java.lang.NullPointerException' exception.
java.lang.NullPointerException: parentLoader == null && !nullAllowed

当我这样做时:

okHttpClient = okHttpClient.newBuilder().addNetworkInterceptor(new Interceptor()
{
    @Override
    public Response intercept(final Chain chain) throws IOException
    {
        final Request request = chain.request();
        Request newRequest;

        newRequest = request.newBuilder()
                            .addHeader(AUTHORIZATION_HEADER, String.format("Bearer %s", "Bob"))
                            .build();
        return chain.proceed(newRequest);

    }
}).build();

0 个答案:

没有答案