0

我有一个改造适配器,其中只有当 acesstoken 不为空时,我才将一个 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);
            }
        });

我的改造适配器代码如下,

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

}

4

1 回答 1

0

通常您将拦截器添加到 http 客户端。拦截器决定添加或不添加标头。为此,您可以按以下方式编写代码:

    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);
        }
    });
于 2016-03-29T09:03:19.997 回答