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
Asked
Active
Viewed 1,169 times
1
-
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 Answers
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
-
That's not possible, he should create an interceptor to do so. He wants to add dynamic value I'd suppose, in case it's static yes, he can do what you say, but otherwise it's not possible. – Skizo-ozᴉʞS ツ Jun 28 '21 at 16:02
-
-
he did not say it's dynamic so I assumed it was needed only for specific api – Mohit Ajwani Jun 29 '21 at 08:20