Home > database >  Retrofit using saved id in SharedPreference to POST update based on id
Retrofit using saved id in SharedPreference to POST update based on id

Time:01-24

I have user id from login saved in shared preference that I want to use for profile update by send POST method to API but getting an error:

Attribute value must be constant

UserService.java:

   GetPrefId callid = new GetPrefId(); //this is the user id source(GetPrefId.java)

   @Headers("Content-Type: application/json")
   @POST("users/update/"   callid)
   Call<UpdateResponse> userUpdate(@Body UpdateRequest updateRequest);

GetPrefId.java:

public class GetPrefId extends AppCompatActivity {

        SharedPrefManager sharedPrefManager = new SharedPrefManager(this);
        private String idPref = sharedPrefManager.getId();

        public static final String prefid() {
                String id = GetPrefId.prefid();
                return id;
        }
}

API endpoint:

apiendpoint.com/users/update/:id

:id is based on saved id in the shared preferences

I don't know any other way to insert the user id to the URL. Thanks for your attention and help

CodePudding user response:

According to retrofit documentation for inserting id in URL do like this @GET("group/{id}/users") Call<List<User>> groupList(@Path("id") int groupId);

CodePudding user response:

You can simply use path to do this, your code will be:

@Headers("Content-Type: application/json")
@POST("users/update/{id}")
Call<UpdateResponse> userUpdate(@Body UpdateRequest updateRequest,@Path("id") int id);

you must pass the user id to userUpdate function when you call it.

  •  Tags:  
  • Related