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
Update:
this is the UpdateActivity logic:
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.setEmail(email.getText().toString());
updateRequest.setUsername(username.getText().toString());
updateRequest.setPassword(password.getText().toString());
Call<UpdateResponse> updateResponseCall = ApiClient.getUserService().userUpdate(updateRequest);
updateResponseCall.enqueue(new Callback<UpdateResponse>() {
@Override
public void onResponse(Call<UpdateResponse> call, Response<UpdateResponse> response) {
if(response.isSuccessful()){
Toast.makeText(EditprofilActivity.this,"Update Successful", Toast.LENGTH_LONG).show();
UpdateResponse signupResponse = response.body();
According to retrofit documentation for inserting id in URL do like this
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);`
Update :
According to your updated question do like this:
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.setEmail(email.getText().toString());
updateRequest.setUsername(username.getText().toString());
updateRequest.setPassword(password.getText().toString());
//here get your id from sharedpref
SharedPrefManager sharedPrefManager = new SharedPrefManager(this);
private String idPref = sharedPrefManager.getId();
//and pass the id to your api function
Call<UpdateResponse> updateResponseCall = ApiClient.getUserService().userUpdate(idPref , updateRequest);