新建一个类UserAgentInterceptor实现Interceptor
- import java.io.IOException;
- import okhttp3.Interceptor;
- import okhttp3.Request;
- import okhttp3.Response;
-
- /**
- * 添加请求头
- */
- public class UserAgentIntercepter implements Interceptor {
- @Override
- public Response intercept(Chain chain) throws IOException {
-
- Request request = chain.request().newBuilder()
- .addHeader("key1","value")
- .build();
-
- return chain.proceed(request);
- }
- }
在okhttputils封装类里添加 拦截器
- .addInterceptor(new UserAgentIntercepter())
-