OkHttp拦截器为空

时间:2019-06-11 10:55:54

标签: java android retrofit2 okhttp3

我有此类可以使用SELECT COLOUR, EMP FROM ( SELECT COLOUR, ROWNUM RN FROM T1 ) T1, ( SELECT EMP, ROWNUM RN FROM T2 ) T2 WHERE MOD(T1.RN, 2) + 1 = T2.RN 创建服务:

retrofit2

我在崩溃报告器中遇到此错误:

public class ServiceGenerator {

    private static Retrofit.Builder builder = new Retrofit.Builder()
            .baseUrl(UrlProvider.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create());

    private static Retrofit retrofit = builder.build();


    private static HttpLoggingInterceptor.Level logLevel = BuildConfig.DEBUG ?
            HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE;

    private static HttpLoggingInterceptor logging =
            new HttpLoggingInterceptor().setLevel(logLevel);

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    public static <S> S createService(Class<S> serviceClass) {
        if (!httpClient.interceptors().contains(logging)) {
            httpClient.addInterceptor(logging);
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request original = chain.request();
                    Request request = original.newBuilder()
                            .header("HEADER_USER_AGENT", "USER_AGENT")
                            .method(original.method(), original.body())
                            .build();

                    return chain.proceed(request);
                }
            });

            httpClient.authenticator(new TokenAuthenticator());
            builder.client(httpClient.build());
            retrofit = builder.build();
        }
        return retrofit.create(serviceClass);
    }
}

在某些设备上和在特殊情况下,会发生此错误。
从不同线程调用java.lang.IllegalStateException: Null interceptor: [null, okhttp3.logging.HttpLoggingInterceptor@85a6f78, *.*.*.e.c@1f1feb6, *.*.*.e.c@66917b7] at java.lang.Thread.run(Thread.java:764) at *.*.*.S.run at *.*.*.Y.a at *.*.*.Y.e at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:1040) at okhttp3.OkHttpClient.<init>(OkHttpClient.java:283) 方法的可能性。
ExecuterService对我们有帮助吗?
如何解决这个错误?请帮助我。

0 个答案:

没有答案