java 如何在没有返回类型或回调的情况下执行 DELETE 请求?[改造]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28970476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to perform a DELETE request without return type or Callback? [Retrofit]
提问by Mark Korzhov
I need perform a DELETE request using Retrofit. So, my code snippet of the interface looks like this:
我需要使用 Retrofit 执行 DELETE 请求。所以,我的界面代码片段如下所示:
@DELETE("/api/item/{id}")
void deleteItem(@Path("id") int itemId);
But I get the error:
但我收到错误:
java.lang.IllegalArgumentException: ApiItem.deleteItem: Must have either a return type or Callback as last argument.
java.lang.IllegalArgumentException:ApiItem.deleteItem:必须将返回类型或回调作为最后一个参数。
However, according to the rules of Rest API, I shouldn't receive any response to DELETE request. How should I specify it in the interface?
但是,根据 Rest API 的规则,我不应该收到对 DELETE 请求的任何响应。我应该如何在界面中指定它?
Thanks.
谢谢。
回答by Konrad Krakowiak
You have to add Callbackas last argument in request method if you want to have void method. You can useCallback<Response>
.
你必须回调添加在请求方法,最后一个参数,如果你想拥有无效的方法。您可以使用Callback<Response>
.
You have to change this:
你必须改变这个:
@DELETE("/api/item/{id}")
void deleteItem(@Path("id") int itemId);
to :
到 :
@DELETE("/api/item/{id}")
void deleteItem(@Path("id") int itemId, Callback<Response> callback);
Or you can return just Response
或者你可以只返回 Response
@DELETE("/api/item/{id}")
Response deleteItem(@Path("id") int itemId);
回答by BurtK
In Retrofit 2.0, You can use Call interface for the result of your request as below.
在 Retrofit 2.0 中,您可以对请求的结果使用 Call 接口,如下所示。
@DELETE("/api/item/{id}")
Call<Response> deleteItem(@Path("id") int itemId);
...
Call<Response> call = YourServiceInstance.deleteItem(10);
call.enqueue(new Callback<Response>() {
...
});
回答by Dishant Kawatra
@FormUrlEncoded
@HTTP(method = "DELETE", path = "manage-feed", hasBody = true)
Call<ResponseBody> deletePost(@Field("post_id") Integer postId, @Field("share_id") Integer sharedMapId);