Retrofit throwing IllegalArgumentException exception for asynchronous FormUrlEncoded DELETE call

前端 未结 2 1095
星月不相逢
星月不相逢 2020-12-16 16:56

I\'m trying to make an asynchronous POST and DELETE which is form url encoded using Retrofit in Android 4.4

Here is my client -

@FormUrlEncoded
@POS         


        
相关标签:
2条回答
  • 2020-12-16 17:20

    Updated answer for Retrofit 2.0:

    Retrofit 2 doesn't seem to have @RestMethod any more, so here is what works:

    @FormUrlEncoded
    @HTTP(method = "DELETE", path = INetwork.API_BASE_PREFIX + "/memberships.json", hasBody = true)
    void leave(@Field("id") String id, Callback<?> cb);
    

    For retrofit 2.+

    @FormUrlEncoded
    @HTTP(method = "DELETE", path = INetwork.API_BASE_PREFIX + "/memberships.json", hasBody = true)
    Callback<?> cb(@Field("id") String id);
    

    and for RxRetrofit 2.+

    @FormUrlEncoded
    @HTTP(method = "DELETE", path = INetwork.API_BASE_PREFIX + "/memberships.json", hasBody = true)
    Observable<?> cb(@Field("id") String id);
    
    0 讨论(0)
  • 2020-12-16 17:27

    The RFC for HTTP is unclear on whether or not the DELETE method is allowed to have a request body or not. Retrofit is raising an error on the side of caution by not having one.

    However, you can still include one (assuming the HTTP client supports it) by using a custom HTTP method annotation.

    package com.myapp;
    
    @Target(METHOD)
    @Retention(RUNTIME)
    @RestMethod(value = "DELETE", hasBody = true)
    public @interface BODY_DELETE {
      String value();
    }
    

    Now specify your interface method using the custom annotation that you defined.

    @FormUrlEncoded
    @BODY_DELETE(INetwork.API_BASE_PREFIX + "/memberships.json")
    void leave(@Field("id") String id, Callback<?> cb);
    
    0 讨论(0)
提交回复
热议问题