'How to change activity while get 401 status code in Interceptor Android

When I am getting 401 status code in API then I have to open login activity. I don't want to put change activity logic in every API's onError method. I want a global method which used for all the API's. So for that, I created one Interceptor

public class MyInterceptor extends BaseActivity implements Interceptor {

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

    if (response.code() == 401) {

        throw new RuntimeException(" Here you got 401 from API !");
    }

    return response;
}
}

Here I add this Interceptor

OkHttpClient.Builder builder=new OkHttpClient.Builder()
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request newRequest = chain.request().newBuilder()
                            .addHeader("Content-Type", "application/json")
                            .addHeader("User-Agent", "MyApp-Android-App")
                            .build();
                    return chain.proceed(newRequest);
                }
            })
            .addNetworkInterceptor(new StethoInterceptor())
            .addNetworkInterceptor(new MyInterceptor())
            .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));

Now, where should I add change activity call method. One more thing I am calling API using RxRetrofit. I want a global method for handling this 401 response. Can you please provide me any solution where should I put Activity change method call?



Solution 1:[1]

create interceptor class

class HeaderInterceptor implements Interceptor
    {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request orignal = chain.request();
///--------------------------
            String token="your auth token"; //retriving depend upon you
//---------------------------

            Request request =orignal.newBuilder().header("Authorization", "Bearer "+ token)
                    .header("Accept", "application/json")
                    .header("Content-Type", "application/json").
                            method(orignal.method(), orignal.body())
                    .build();

            Response response =  chain.proceed(request);
            Log.d("MyApp", "Code : "+response.code());
            if (response.code() == 401){
                //handle it as per ur need
                return response;
            }
            return response;
        }
    }

now add interceptor

.....
                .addInterceptor(logging).addInterceptor(new HeaderInterceptor())
                .build();
    ..

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhishek