Noe Nazza
Noe Nazza

Reputation: 61

Apply variable to Retrofit HTTP method annotation

This is the code. I want put params EMAIL! But i get attribute value must be costant

public interface Api{
    Context context=null;
    SharedPreferences sharedPref = context.getSharedPreferences("login",Context.MODE_PRIVATE);
    static String email = sharedPref.getString("email","");

    @GET("Home.php?email="+email+"&")
    Call<List<Variabili_Main>> getHome(@Query("page") int index);

}

Help me!

ok i have edit but now how edit this? And here what i do?

private void load(int index){
    Call<List<Variabili_Main>> call = api.getHome(index);
    call.enqueue(new Callback<List<Variabili_Main>>() {
        @Override
        public void onResponse(Call<List<Variabili_Main>> call, Response<List<Variabili_Main>> response) {
            if(response.isSuccessful()){
                movies.addAll(response.body());
                adapter.notifyDataChanged();
            }else{
                Log.e(TAG," Response Error "+String.valueOf(response.code()));
            }
        }

        @Override
        public void onFailure(Call<List<Variabili_Main>> call, Throwable t) {
            Log.e(TAG," Response Error "+t.getMessage());
        }
    });
}

Upvotes: 4

Views: 4650

Answers (3)

Hala.M
Hala.M

Reputation: 766

After rereading the question @cricket_007 was correct since the email was to be sent as a query

I'll update it for future reference to have both answers

I think this is how it should be I has similar issue with header before

try this solution

this is if the email is in the path itself not a query

@GET("Home.php/{email}")
Call<List<Variabili_Main>> getHome(@Path("email") String email,@Query("page") int index);

this is to send it as a query

@GET("Home.php")
Call<List<Variabili_Main>> getHome(@Query("email") String email,@Query("page") int index);

to call you need to add the email in your call

 SharedPreferences sharedPref = context.getSharedPreferences("login",Context.MODE_PRIVATE);
static String email = sharedPref.getString("email","");
api.getHome(email,index)

check for more details Dynamic Paths in Retrofit and https://square.github.io/retrofit/

Upvotes: 3

Tropid
Tropid

Reputation: 121

As stated in your comments your goal is to handle a HTTP GET request to a specific route where the email query parameter is determined at runtime (loaded from shared preferences).

To achieve this goal you should use the @Query notation provided by the retrofit library.

Example from https://square.github.io/retrofit/ "API Declaration"

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);

In your case you can pass the email value as argument to your handler function and continue from there. E.g.

@GET("Home.php")
Call<List<Variabili_Main>> getHome(@Query("page") int index, @Query("email") String email);

Why it is not possible to provide the email in the annotation:

As stated in my comment providing runtime strings to annotations is not possible as stated in the JLS 9.7.1:

  • If T is a primitive type or String, and V is a constant expression
    (§15.28).
  • V is not null.
  • If T is Class, or an invocation of Class, and V is a class literal (§15.8.2).
  • If T is an enum type, and V is an enum constant.

Where V is the value inside the annotation.

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191844

If you want to concatenate a string in the annotation, it must be a compile-time constant. Something that is received at run-time from SharedPreferences isn't constant.

Probably better to add it as a query parameter.

@GET("Home.php")
Call<List<Variabili_Main>> getHome(@Query("email") String email, @Query("page") int index);

Then, just pass the email when you actually need it.

private void load(int index){

    // TODO: getContext ... shared preferences... 
    String email = prefs.getString("email");
    Call<List<Variabili_Main>> call = api.getHome(email, index);

    call.enqueue(new Callback<List<Variabili_Main>>() {
        @Override
        public void onResponse(Call<List<Variabili_Main>> call, Response<List<Variabili_Main>> response) {
            if(response.isSuccessful()){
                movies.addAll(response.body());
                adapter.notifyDataChanged();
            }else{
                Log.e(TAG," Response Error "+String.valueOf(response.code()));
            }
        }

        @Override
        public void onFailure(Call<List<Variabili_Main>> call, Throwable t) {
            Log.e(TAG," Response Error "+t.getMessage());
        }
    });
}

Upvotes: 1

Related Questions