1

I I tried to call the API key that I created with nativeLib, but when it is called in the retrofit header it can't be executed and there is a message "Attribute value must be constant"

APIInterface

String RAPID_API_KEY = NativeLib.apiKey();
String RAPID_API_HOST = NativeLib.apiHost();

@Headers({RAPID_API_KEY, RAPID_API_HOST}) // Error message in this line
@GET
Call<Post>getVideoByUrl(@Url String url, @Query("url") String inputUrl);

is there a solution for this problem? I really appreciate whatever the answer is.

2 Answers 2

1

Annotation attribute value must be known at compile-time, only inlined compile-time constants are allowed, like this:

/* static final */ String RAPID_API_KEY = "X-Header-Name: RtaW4YWRtaYWucG..."
/* static final */ String RAPID_API_KEY = "X-Header-Name: " + "RtaW4YWRtaYWucG..."
/* static final */ String RAPID_API_KEY = "X-Header-Name: " + Constants.API_KEY /* API_KEY = "RtaW4YWRtaYWucG..." */

but not

String RAPID_API_KEY = NativeLib.apiKey(); /* Here compiler can't compute NativeLib.apiKey() value */

Alternatively, you can pass header in parameter dynamically using @Header or @HeaderMap

@GET
Call<Post> getVideoByUrl(@HeaderMap Map<String, String> headers,
                         @Url String url,
                         @Query("url") String inputUrl);
...
final Map<String, String> headers = new HashMap<>();
headers.put("Key Header Name", NativeLib.apiKey());
headers.put("Host Header Name", NativeLib.apiHost());
...
api.getVideoByUrl(headers, ...);

also you can use an OkHttp interceptor if you want to add the header to all requests.

0

You should try to add "" before NativeLib.apiKey(); and same in NativeLib.apiHost();. please refer here https://stackoverflow.com/a/39157786/12660050 and In Java why this error: 'attribute value must be constant'?

2
  • I've tried String RAPID_API_KEY = "" + NativeLib.apiKey(); but the result is still the same Commented Aug 27, 2021 at 5:48
  • you have given the key of headers value ? Like @Headers("Key Name: RAPID_API_KEY") Commented Aug 27, 2021 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.