Android - Retrofit2 - 403-Forbidden

时间:2016-12-15 12:08:02

标签: android retrofit2

我正在使用retrofit2为网络调用创建一个应用程序。我需要在Single Activity中调用多个API。现在我面临 403-forbidden 错误。如果我只调用一个API,它工作正常。但是,如果我逐个使用多个API调用,那么我将面临此错误。

我的CreateService方法如下:

public static <S> S createService(Class<S> serviceClass, final String authToken) {
        if (authToken != null) {
            httpClient.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request original = chain.request();

                    // Request customization: add request headers
                    Request.Builder requestBuilder = original.newBuilder()
                            .header("Authorization-Token", authToken)
                            .method(original.method(), original.body());

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

//        OkHttpClient client = httpClient.readTimeout(60, TimeUnit.SECONDS).connectTimeout(60, TimeUnit.SECONDS).build();
        Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(200));
        dispatcher.setMaxRequests(200);
        dispatcher.setMaxRequestsPerHost(1);

        OkHttpClient okHttpClient = httpClient.dispatcher(dispatcher).connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)).build();

        Retrofit retrofit = builder.client(okHttpClient).build();
        return retrofit.create(serviceClass);
    }

我的代码有什么问题..我该如何处理?

1 个答案:

答案 0 :(得分:2)

你确定字符串不是空字符串吗?

您能否添加日志拦截器并设置日志级别并提供日志?

compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

就像这样:

OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
    okBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC).setLevel
            (HttpLoggingInterceptor.Level.BODY).setLevel(HttpLoggingInterceptor.Level.HEADERS))