How to perform a DELETE request without return type or Callback? [Retrofit]

后端 未结 4 1631
长发绾君心
长发绾君心 2021-02-20 11:07

I need perform a DELETE request using Retrofit. So, my code snippet of the interface looks like this:

@DELETE(\"/api/item/{id}\")
void deleteItem(@Path(\"id\") i         


        
相关标签:
4条回答
  • 2021-02-20 11:31

    In Retrofit 2.0, You can use Call interface for the result of your request as below.

    @DELETE("/api/item/{id}")
    Call<Response> deleteItem(@Path("id") int itemId);
    
    ...
    
    Call<Response> call = YourServiceInstance.deleteItem(10);
    call.enqueue(new Callback<Response>() {
    ...
    });
    
    0 讨论(0)
  • 2021-02-20 11:35
    @FormUrlEncoded
    @HTTP(method = "DELETE", path = "manage-feed", hasBody = true)
    Call<ResponseBody> deletePost(@Field("post_id") Integer postId, @Field("share_id") Integer sharedMapId);
    
    0 讨论(0)
  • 2021-02-20 11:36

    You have to add Callback as last argument in request method if you want to have void method. You can useCallback<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

    @DELETE("/api/item/{id}")
    Response deleteItem(@Path("id") int itemId);
    
    0 讨论(0)
  • 2021-02-20 11:37
        @HTTP(method = "DELETE", path = "/api/item/{id}", hasBody = false)
    fun deleteItemId(
    @Path("id") id: Int
    ) : Call<YourResponse>
    
    hasBody = 
    true -> if api has request body
    false -> if api has not a request body
    
    0 讨论(0)
提交回复
热议问题