builder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
SharedPreferences preferences = MyApp.getApplication().getSharedPreferences("user", Context.MODE_PRIVATE);
String userId = preferences.getString("UserId", "");
String sessionId = preferences.getString("SessionId", "");
Request.Builder builder1 = request.newBuilder();
builder1.method(request.method(),request.body());
if(!TextUtils.isEmpty(userId)&&!TextUtils.isEmpty(sessionId)){
builder1.addHeader("userId",userId);
builder1.addHeader("sessionId",sessionId);
}
Request build = builder1.build();
return chain.proceed(build);
}
});
自定义拦截器(请求头)
最新推荐文章于 2024-08-22 09:22:35 发布