使用 SharedPreference 中保存的 id 改造为 POST 基于 id 的更新
Retrofit using saved id in SharedPreference to POST update based on id
我将登录的用户 ID 保存在共享首选项中,我想通过将 POST 方法发送到 API 以用于配置文件更新,但出现错误:
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 端点:
apiendpoint.com/users/update/:id
:id 基于共享首选项中保存的 id
我不知道还有什么其他方法可以将用户 ID 插入 URL。感谢您的关注和帮助
更新:
这是 UpdateActivity 逻辑:
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();
根据 retrofit documentation 在 URL 中插入 id,请这样做
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);`
更新:
根据你更新的问题,这样做:
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);
我将登录的用户 ID 保存在共享首选项中,我想通过将 POST 方法发送到 API 以用于配置文件更新,但出现错误:
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 端点:
apiendpoint.com/users/update/:id
:id 基于共享首选项中保存的 id
我不知道还有什么其他方法可以将用户 ID 插入 URL。感谢您的关注和帮助
更新:
这是 UpdateActivity 逻辑:
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();
根据 retrofit documentation 在 URL 中插入 id,请这样做
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);`
更新:
根据你更新的问题,这样做:
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);