OkHttpClient.Builder Retrofit 2 添加拦截器,即使条件为 false
OkHttpClient.Builder Interceptor is added to retrofit 2,even condition is false
我有一个改造适配器,其中只有当访问令牌不为空时,我才将访问令牌拦截器附加到每个请求,但每个请求都附加了拦截器。
我的要求如下,
if(null!= ApplicationClass.getAccessToken())
CLIENT.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("authorization", ApplicationClass.getAccessToken()).build();
return chain.proceed(request);
}
});
我的Retrofit Adapter代码如下,
import android.support.annotation.NonNull;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.concurrent.TimeUnit;
import cl.tt.ApplicationClass;
import cl.tt.BuildConfig;
import cl.tt.modals.MActionDone;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
public class RetrofitAdapters {
private static OkHttpClient.Builder CLIENT;
private static String ROOT = BuildConfig.HOST + "/api/v1/";
static {
CLIENT = new OkHttpClient().newBuilder();
CLIENT.connectTimeout(1200, TimeUnit.SECONDS);
CLIENT.readTimeout(1200, TimeUnit.SECONDS);
CLIENT.writeTimeout(1200, TimeUnit.SECONDS);
}
@NonNull
public static <S> S createRetrofitService(Class<S> serviceClass) {
return getRetrofit().create(serviceClass);
}
@NonNull
private static Retrofit getRetrofit() {
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
CLIENT.addInterceptor(interceptor);
}
if(null!= ApplicationClass.getAccessToken())
CLIENT.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("authorization", ApplicationClass.getAccessToken()).build();
return chain.proceed(request);
}
});
return new Retrofit.Builder()
.baseUrl(ROOT)
.addConverterFactory(JacksonConverterFactory.create())
.client(CLIENT.build())
.build();
}
public static MActionDone parseError(Response<?> response) {
Converter<ResponseBody, MActionDone> converter =
getRetrofit().responseBodyConverter(MActionDone.class, new Annotation[0]);
MActionDone error;
try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
return new MActionDone();
}
return error;
}
}
通常您将拦截器添加到 http-client。而拦截器决定添加或不添加headers。为此,您可以按以下方式编写代码:
CLIENT.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String token = ApplicationClass.getAccessToken();
if (token != null && !token.isEmpty()) {
request = request.newBuilder().addHeader("authorization", token).build();
}
return chain.proceed(request);
}
});
我有一个改造适配器,其中只有当访问令牌不为空时,我才将访问令牌拦截器附加到每个请求,但每个请求都附加了拦截器。
我的要求如下,
if(null!= ApplicationClass.getAccessToken())
CLIENT.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("authorization", ApplicationClass.getAccessToken()).build();
return chain.proceed(request);
}
});
我的Retrofit Adapter代码如下,
import android.support.annotation.NonNull;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.concurrent.TimeUnit;
import cl.tt.ApplicationClass;
import cl.tt.BuildConfig;
import cl.tt.modals.MActionDone;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
public class RetrofitAdapters {
private static OkHttpClient.Builder CLIENT;
private static String ROOT = BuildConfig.HOST + "/api/v1/";
static {
CLIENT = new OkHttpClient().newBuilder();
CLIENT.connectTimeout(1200, TimeUnit.SECONDS);
CLIENT.readTimeout(1200, TimeUnit.SECONDS);
CLIENT.writeTimeout(1200, TimeUnit.SECONDS);
}
@NonNull
public static <S> S createRetrofitService(Class<S> serviceClass) {
return getRetrofit().create(serviceClass);
}
@NonNull
private static Retrofit getRetrofit() {
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
CLIENT.addInterceptor(interceptor);
}
if(null!= ApplicationClass.getAccessToken())
CLIENT.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("authorization", ApplicationClass.getAccessToken()).build();
return chain.proceed(request);
}
});
return new Retrofit.Builder()
.baseUrl(ROOT)
.addConverterFactory(JacksonConverterFactory.create())
.client(CLIENT.build())
.build();
}
public static MActionDone parseError(Response<?> response) {
Converter<ResponseBody, MActionDone> converter =
getRetrofit().responseBodyConverter(MActionDone.class, new Annotation[0]);
MActionDone error;
try {
error = converter.convert(response.errorBody());
} catch (IOException e) {
return new MActionDone();
}
return error;
}
}
通常您将拦截器添加到 http-client。而拦截器决定添加或不添加headers。为此,您可以按以下方式编写代码:
CLIENT.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String token = ApplicationClass.getAccessToken();
if (token != null && !token.isEmpty()) {
request = request.newBuilder().addHeader("authorization", token).build();
}
return chain.proceed(request);
}
});