okhttp网络拦截器

新建类 实现 Interceptor

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

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;
    }
}

在OKHttpUtils中 新建OKHttp是 加入拦截器

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

猜你喜欢

转载自blog.csdn.net/qq_42535851/article/details/82017957