简体   繁体   中英

Why am I getting IllegalArgumentException error while deleting data by id by Retrofit2? And how to fix it?

I am trying to delete an user in phpmyadmin by id parameter by Java, Retrofit, PHP, MySQL. When I press the delete button, in the Logcat I am getting this 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 : Here is the listener for delete button:

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();
                    }
            });

Why am I getting this error? And How to fix it. Thank You in Advance.

Replace this:

 @DELETE("delete_user.php?id={id}")
Call<Void> deletePost(@Path("id") int id);

With this

 @DELETE("delete_user.php")
Call<Void> deletePost(@Query("id") int id);

@Path is used for building a dynamic endpoint, but for Query's you need to query annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM