Android Retrofit无法解析符号AuthenticationInterceptor

时间:2017-07-10 13:23:11

标签: java android gradle retrofit

我正在创建一个简单的改造,但是我收到了这个错误:

  

无法解析符号AuthenticationInterceptor

下面:

public static <S> S createService(
        Class<S> serviceClass, final String authToken) {
    if (!TextUtils.isEmpty(authToken)) {
        AuthenticationInterceptor interceptor =
                new AuthenticationInterceptor(authToken);

        if (!httpClient.interceptors().contains(interceptor)) {
            httpClient.addInterceptor(interceptor);

            builder.client(httpClient.build());
            retrofit = builder.build();
        }
    }

    return retrofit.create(serviceClass);
}

我的构建gradle看起来像这样:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.keiferstone:nonet:2.0.4'
    compile 'com.github.GrenderG:Toasty:1.2.5'
    compile 'com.lmntrx.livin.library.droidawesome:droid-awesome:1.1.9'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'me.pushy:sdk:1.0.28'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
}

1 个答案:

答案 0 :(得分:2)

您似乎使用了this example

在评论中:

  

我使用改装2但是AuthenticationInterceptor是红色的,当我把光标放在上面时我有消息,如无法解析符号&#39; AuthenticationInterceptor&#39;

答案:

  

说实话,AuthenticationInterceptor是一个自定义的Interceptor实现。我会尽快更新代码段以包含此类。

在你的代码中添加它,应该没问题:

public class AuthenticationInterceptor implements Interceptor {

    private String authToken;

    public AuthenticationInterceptor(String token) {
        this.authToken = token;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();

        Request.Builder builder = original.newBuilder()
                .header("Authorization", authToken);

        Request request = builder.build();
        return chain.proceed(request);
    }
}