簡體   English   中英

為什么在 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 中我收到此錯誤

 java.lang.IllegalArgumentException: URL query string "id={id}" must not have replace block. For dynamic query parameters use @Query.
    for method UsersAPI.deletePost

用戶API.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,您需要查詢注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM