1

I've tried so many times with different type of initialisation. In this Image I use a global variable which already initialise a value. Its too much can anybody help with that type of error

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • To add a dynamic header in retrofit, you need to create an interceptor. This question it was solved on stockoverflow -> https://stackoverflow.com/questions/32963394/how-to-use-interceptor-to-add-headers-in-retrofit-2-0 – Manuel Mato Jun 28 '21 at 15:40

2 Answers2

0

You need to create your own interceptor and add the token there not on your interface.

public class AuthorizationHeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        LoginResponse toc = LoginResponse();
        Request request = chain.request()
                .newBuilder()
                .addHeader("Authorization", "Bearer " + toc.getToken())
                .build();
        Response response = chain.proceed(request);
        return response;
    }
}

Then add this interceptor in your OkHttpClient and then add that OkHttpClient to your Retrofit.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
-1

Header accepts the data in this format

Single example:

@Headers({"Accept: application/json"})

Multiple example:

@Headers({
        "Accept: application/json",
        "User-Agent: Your-App-Name",
        "Cache-Control: max-age=640000"
    })

In your case the code should be changed to,

@Headers({"Authorization: "+ tokenn})
Mohit Ajwani
  • 1,328
  • 12
  • 24