Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Retrofit2 - 403-Forbidden

I am creating an application with retrofit2 for network calls. I need call multiple API in Single Activity. Now I am facing the 403-forbidden error. If I call only one API it is working fine. But if I use multiple API calls one by one then I am facing this error.

My CreateService method is below:

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

What is wrong in my code.. How can I handle this?

like image 808
Amsheer Avatar asked Dec 15 '16 12:12

Amsheer


1 Answers

Are you sure that string is not empty string?

Could you please add log interceptor and set the log level and provide a log?

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

And sth like this :

OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
    okBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC).setLevel
            (HttpLoggingInterceptor.Level.BODY).setLevel(HttpLoggingInterceptor.Level.HEADERS))
like image 131
Robert Banyai Avatar answered Nov 10 '22 10:11

Robert Banyai