改造拦截器错误

时间:2016-02-07 08:58:48

标签: android retrofit2

我想将facebook访问令牌添加到改装(2 beta 3)请求中,但未添加访问令牌。 我可以添加拦截器来成功改造1.9但是在改造2中它有错误,有没有解决方案?

protected Retrofit getRestAdapter() {
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Interceptor.Chain chain) throws IOException {
                            Request original = chain.request();

                            String sessionId = getSessionId(); // get access token

                            // Request customization: add request headers
                            Request.Builder requestBuilder = original.newBuilder()
                                    .header("Cookie", sessionId)
                                    .method(original.method(), original.body());

                            Request request = requestBuilder.build();
                            return chain.proceed(request);
                        }
                    })
            .build();

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl("http://tbkha.com/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }

    return retrofit;
}

1 个答案:

答案 0 :(得分:1)

在改造改造2.0中,你可以像这样添加拦截器:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging).build();

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseURL)
                .client(client)
                .build();

点击此链接了解详情https://futurestud.io/blog/retrofit-2-log-requests-and-responses

类似的问题:

App crash on HttpLoggingInterceptor

Retrofit2 HttpLoggingInterceptor Logcat