// 获取ok
private static OkHttpClient okHttpClient;
private static synchronized OkHttpClient getOkHttpClient() {
// 拦截器
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("mmm", message);
}
});
okHttpClient = new OkHttpClient
.Builder()
.addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder()
.header("userId", "11249")
.header("sessionId", "155056366467311249");
Request build = builder.build();
return chain.proceed(build);
}
})
.connectTimeout(3000, TimeUnit.MILLISECONDS)
.readTimeout(3000, TimeUnit.MILLISECONDS)
.build();
return okHttpClient;
}