Android Retrofit 2 - GET - error: element value must be a constant expression
Hi,
I have set GET for Retrofit2 in my android application as follows:
And it works. However, I need to get a dynamic URL into the GET.
If instead of static final variables (public static final String)
I will use a non-static variable here via the class Variables:
So I get an error:
error: element value must be a constant expression
@GET(Constants.API_URL + Variables.getType())
Can anyone advise me how to get a dynamic URL variable into the GET URL for Retrofit2 in Android Studio?
Thanks
Hi,
if in Android Studio you need to get a non-static variable for a dynamic URL for an Android application using Retrofit2 in GET, then do it like this, it works!
I have set GET for Retrofit2 in my android application as follows:
@GET(Constants.API_URL + Constants.TYPE)
Call> getApiDatas(@Header("X-AUTH-TOKEN") String token);
.......
Call> call = APIService.getApiDatas(Constants.TOKEN_KEY);
.......
And it works. However, I need to get a dynamic URL into the GET.
If instead of static final variables (public static final String)
@GET(Constants.API_URL + Constants.TYPE)
I will use a non-static variable here via the class Variables:
@GET(Constants.API_URL + Constants.TYPE)
So I get an error:
error: element value must be a constant expression
@GET(Constants.API_URL + Variables.getType())
Can anyone advise me how to get a dynamic URL variable into the GET URL for Retrofit2 in Android Studio?
Thanks
REPLY
Hi,
if in Android Studio you need to get a non-static variable for a dynamic URL for an Android application using Retrofit2 in GET, then do it like this, it works!
@GET
Call> getApiDatas(@Header("X-AUTH-TOKEN") String token, @Url String url);
.......
Call> call = APIService.getApiDatas(Constants.TOKEN_KEY, Constants.TYPE + Variables.getType());
.......
dodatek - jak lze posílat requesty když používáme android retrofit a chceme do url předávat dynamicky parametry.
Endpoint má třeba tento tvar a chceme dynamicky dodávat param "barcode" :
http://192.168.35.205:8001/api/endpoint/get?barcode=B0002
1.) předávání parametrů "barcode=B0002" přes anotaci
-----------
String endpoint = Constants.URL+"endpoint/get";
Call<List<ApiData>> call = APIService.getApiDatas(endpoint, auth, Constants.TOKEN_KEY, code);
interface APIService {
@GET
Call<List<ApiData>> getApiDatas(
@Url String url,
@Header("Authorization") String auth,
@Header("X-AUTH-TOKEN") String token,
@Query("barcode") String barcode
);
}
2.) předávání parametrů "barcode=B0002" už rovnou v názvu endpointu (string)
------------
String endpoint = Constants.URL+"endpoint/get?barcode="+code;
Call<List<ApiData>> call = APIService.getApiDatas(endpoint, auth, Constants.TOKEN_KEY);
interface APIService {
@GET
Call<List<ApiData>> getApiDatas(
@Url String url,
@Header("Authorization") String auth,
@Header("X-AUTH-TOKEN") String token
);
}