为什么我在通过 Retrofit2 按 id 删除数据时出现 IllegalArgumentException 错误?以及如何解决?
Why am I getting IllegalArgumentException error while deleting data by id by Retrofit2? And how to fix it?
我正在尝试通过 Java、Retrofit、PHP、MySQL 通过 id 参数删除 phpmyadmin 中的用户。当我按下删除按钮时,在 Logcat 我得到这个 ERROR:
java.lang.IllegalArgumentException: URL query string "id={id}" must not have replace block. For dynamic query parameters use @Query.
for method UsersAPI.deletePost
UserAPI.java :
import retrofit2.Call;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface UsersAPI {
@GET("users_read.php")
Call<List<User>> getUsers();
@DELETE("delete_user.php?id={id}")
Call<Void> deletePost(@Path("id") int id);
}
AdminActivity.java :这是删除按钮的侦听器:
btnDeleteUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int deleteId = Integer.parseInt(editTextDeleteUser.getText().toString());
Call<Void> call = usersAPI.deletePost(deleteId);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Toast.makeText(AdminActivity.this,"Response code: "+response.code(),Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast.makeText(AdminActivity.this, "Error : "+ t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
为什么会出现此错误?以及如何修复它。提前致谢。
替换为:
@DELETE("delete_user.php?id={id}")
Call<Void> deletePost(@Path("id") int id);
有了这个
@DELETE("delete_user.php")
Call<Void> deletePost(@Query("id") int id);
@Path 用于构建动态端点,但对于Query,您需要查询注释。
我正在尝试通过 Java、Retrofit、PHP、MySQL 通过 id 参数删除 phpmyadmin 中的用户。当我按下删除按钮时,在 Logcat 我得到这个 ERROR:
java.lang.IllegalArgumentException: URL query string "id={id}" must not have replace block. For dynamic query parameters use @Query.
for method UsersAPI.deletePost
UserAPI.java :
import retrofit2.Call;
import retrofit2.http.DELETE;
import retrofit2.http.GET;
import retrofit2.http.Path;
public interface UsersAPI {
@GET("users_read.php")
Call<List<User>> getUsers();
@DELETE("delete_user.php?id={id}")
Call<Void> deletePost(@Path("id") int id);
}
AdminActivity.java :这是删除按钮的侦听器:
btnDeleteUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int deleteId = Integer.parseInt(editTextDeleteUser.getText().toString());
Call<Void> call = usersAPI.deletePost(deleteId);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
Toast.makeText(AdminActivity.this,"Response code: "+response.code(),Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast.makeText(AdminActivity.this, "Error : "+ t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
为什么会出现此错误?以及如何修复它。提前致谢。
替换为:
@DELETE("delete_user.php?id={id}")
Call<Void> deletePost(@Path("id") int id);
有了这个
@DELETE("delete_user.php")
Call<Void> deletePost(@Query("id") int id);
@Path 用于构建动态端点,但对于Query,您需要查询注释。