okhttp中Interceptor的使用

在Retrofit2中使用okhttp的Interceptor来插入Header自定义参数

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

                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Accept", "application/vnd.github.mercy-preview+json"); // <-- this is the important line

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

        OkHttpClient client = httpClient.build();

猜你喜欢

转载自blog.csdn.net/u013648164/article/details/73729938

相关文章