Android Retrofit @DELETE请求报IllegalArgumentException: Non-body HTTP method cannot contain @Body错误解决方法

使用Retrofit创建一个删除用户的方法,传的是json数据,结果报Non-body HTTP method cannot contain @Body错误,意思就是@DELETE方法请求不支持传递@Body

@DELETE("/user/delete")
Observable<Object> deleteUser(@Body User body);
01-05 12:56:43.016 8717-8717/com.himmy.myapp E/BugUtils: java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
        for method HttpService.deletePositions
        at retrofit2.Utils.methodError(Utils.java:52)
        at retrofit2.Utils.methodError(Utils.java:42)
        at retrofit2.RequestFactory$Builder.build(RequestFactory.java:189)
        at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:397)
        at $Proxy4.deletePositions(Unknown Source)
        at com.xindecoiot.locamgr.yunsh.http.HttpServiceImpl.deletePositions(HttpServiceImpl.java:153)
        at com.xindecoiot.locamgr.yunsh.LocaListActivity.postDeleteLoca(LocaListActivity.java:379)
        at com.xindecoiot.locamgr.yunsh.LocaListActivity.access$100(LocaListActivity.java:40)
        at com.xindecoiot.locamgr.yunsh.LocaListActivity$2$2$1.onClick(LocaListActivity.java:149)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5869)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1019)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:814)

解决方法很简单,只需设置@DELETE方法支持传递body报文即可,如下代码所示

@HTTP(method = "DELETE", path = "/user/delete", hasBody = true)
Observable<Object> deleteUser(@Body User body);

 

 

 

posted @ 2020-01-05 13:09  野猿新一  阅读(278)  评论(0编辑  收藏  举报