【问题标题】:Custom HTTP method not working in Retrofit 2自定义 HTTP 方法在 Retrofit 2 中不起作用
【发布时间】:2018-07-10 09:42:26
【问题描述】:

您好,我在我的代码中使用了自定义方法,如下所示,但它总是给我

java.lang.IllegalArgumentException:方法 AUTH 不能有 请求正文。

我的代码不工作它总是说:Custom method AUTH, must not have a Body

@Headers("Content-Type: application/json") 
@HTTP(method = "AUTH", path = "login/{deviceId}", hasBody = true) 
Call<Success> getLogin( @Path("deviceId") int deviceId, @Body RequestBody password); 

我正在使用以下依赖项。

compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

你能帮帮我吗?

【问题讨论】:

  • 所以删除@Body RequestBody password。你应该做的请求方法是什么? GET?POST?PUT?
  • 我需要将密码作为原始数据传递给正文:{"password":"*****"}。我也在使用自定义 http 方法,如:github.com/square/retrofit/blob/…
  • 发送请求的 uri 是什么?是 auth/login/{deviceId} 吗?你必须执行什么方法?
  • 不,URL 是:serverurl/login{deviceId} 并且 AUTH 是自定义方法名称
  • 你为什么要编造http方法?阅读softwareengineering.stackexchange.com/questions/193821/…tldr 不要使用自定义方法

标签: java android retrofit2 okhttp3


【解决方案1】:

我也遇到了同样的问题,查了文档发现问题是retrofit版本,试试2.4.0或者更高版本。

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多