改型URL查询字符串不能包含replace块

时间:2018-07-25 06:00:57

标签: android retrofit

我已经创建了一种使用Retrofit2删除MySQL数据库中某些数据的方法,但是它总是会引发如下错误:

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

这是我的deleteBooking方法:

public void deleteBooking(int id){
        Call<BookingInfo> call = userService.deleteBooking(id);
        call.enqueue(new Callback<BookingInfo>() {
            @Override
            public void onResponse(Call<BookingInfo> call, Response<BookingInfo> response) {
                if(response.isSuccessful()){
                    Log.i("DELETE RESPONSE: ", response.body().toString());
                    finish();
                }
            }

            @Override
            public void onFailure(Call<BookingInfo> call, Throwable t) {
                    Log.e("DELETE ERROR: ", t.getMessage());
            }
        });
    }

这是我的UserService界面:

public interface UserService {

    @GET("api.php?apicall=getBookings")
    Call<GetBookingInfo> getBookings();

    @FormUrlEncoded
    @POST("api.php?apicall=createBooking")
    Call<BookingInfo> addBooking(@FieldMap HashMap<String, String> booking);

    @PUT("api.php?apicall=updateStatus")
    Call<BookingInfo> updateStatus(@Path("status") String status, @Body BookingInfo booking);

    @DELETE("api.php?apicall=deleteBooking&={id}")
    Call<BookingInfo> deleteBooking(@Query("id") int id);

}

我将调用deleteBooking更改为@Query(我已经搜索了几个问题,答案总是必须更改为@Query,在此之前我使用@Path),但仍然会引发错误。我不知道解决方案。如果您想了解更多信息,请随时询问,我会给您您所需的代码,等等。谢谢!

1 个答案:

答案 0 :(得分:0)

将其更改为以下内容:

@DELETE("api.php")
Call<BookingInfo> deleteBooking(@Query("id") int id,
                                @Query("apicall") String action);

并按以下方式调用它:

Call<BookingInfo> call = userService.deleteBooking(id, "deleteBooking");

编辑: 如果您不想移出该动作,请尝试以下操作:

     @DELETE("api.php?apicall=deleteBooking")
     Call<BookingInfo> deleteBooking(@Query("id") int id);