【问题标题】:Why am I getting IllegalArgumentException error while deleting data by id by Retrofit2? And how to fix it?为什么我在 Retrofit2 按 id 删除数据时收到 IllegalArgumentException 错误?以及如何解决?
【发布时间】:2020-05-26 14:28:17
【问题描述】:

我正在尝试通过 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();
                    }
            });

为什么会出现此错误?以及如何解决它。提前谢谢你。

【问题讨论】:

    标签: java php android retrofit2


    【解决方案1】:

    替换这个:

     @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,您需要查询注释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 2017-07-16
      • 1970-01-01
      相关资源
      最近更新 更多