OkHttpClient.Builder拦截器被添加到改造2,甚至条件是假的

时间:2016-03-29 08:30:15

标签: retrofit okhttp retrofit2 okhttp3

我有一个改装适配器,只有当acesstoken不为null时,我才会在每个请求中附加一个accestoken的拦截器,但拦截器会附加每个请求。

我的要求如下,

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

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

}

1 个答案:

答案 0 :(得分:0)

通常你会将拦截器添加到http-client。拦截器决定添加或不添加标头。为此,您可以用下一种方式编写代码:

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