添加一个简单的okhttp网络拦截器

先定义个实现intercept的类

public class MyInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request build = request.newBuilder().addHeader("source", "android").build();
        Response proceed = chain.proceed(build);
        return proceed;
    }
}

在okhttp的请求方法里添加

OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();

 

你可能感兴趣的:(添加一个简单的okhttp网络拦截器)