1

I have an interface that has a static key. And I wanted to retrieve this key from a string file, but if I just put R.string.key, it shows incompatible types, because it retrieves the integer value, and if I put R.string.key + "", it becomes a string but retrieves the String file id, it would be best to use getResources.getString (R.string.key), but there is no way to use the getResources.getString method in the Interface.

Working:

public interface NotificacaoService {
    @Headers({"34853485734",
            "Content-Type:application/json"}) @POST("send")
    Call<NotificacaoDados> salvarNotificacao(@Body NotificacaoDados notificacaoDados);
}

I want to leave it like this:

public interface NotificacaoService {
    @Headers({getResources.getString(R.string.key),
            "Content-Type:application/json"}) @POST("send")
    Call<NotificacaoDados> salvarNotificacao(@Body NotificacaoDados notificacaoDados);
}

1 Answer 1

1

Sorry, what you want is not possible. Annotation parameters need to be constants.

One solution is to switch from string resources to BuildConfig. Use buildConfigField in your Gradle script to define your key (buildConfigField "String", "API_KEY", "\"34853485734\""). Then, you can reference that generated constant in your interface (e.g.,BuildConfig.API_KEY`).

Alternatively — if this interface is for Retrofit — you could add your headers via an OkHttp interceptor, instead of via a @Headers annotation.

Sign up to request clarification or add additional context in comments.

1 Comment

I was able to use the Gradle file as you said, thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.