Android开发okhttp添加头部参数或者是头文件
private static class RequestHeaderInterceptor implements Interceptor {@Overridepublic Response intercept(Chain chain) throws IOException {Request original = chain.request();//添加头部信息Request request = original.newBuilder()//header()如果有重名的将会覆盖,而addHeader()允许相同key值的header存在.header("channel", BuildConfig.FLAVOR).header("versionId", BuildConfig.VERSION_ID + "").header("platformType", "1").header("platformInfo", android.os.Build.MODEL + "-------" + android.os.Build.VERSION.RELEASE).header("token", InfoUtils.getToken()).header("package", BuildConfig.APPLICATION_ID).header("user-agent", System.getProperty("http.agent")).method(original.method(), original.body()).build();Response response = chain.proceed(request);return response;}}
使用示例:
builder.addInterceptor(requestHeaderInterceptor);