Java 带有请求正文 RETROFIT 的 HTTP GET
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29834959/
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
HTTP GET with request body RETROFIT
提问by user2026760
I am using Retrofit to make api calls in my android application. I have to submit a @Body of JSON
我正在使用 Retrofit 在我的 android 应用程序中进行 api 调用。我必须提交一个@Body of JSON
@GET("api/")
void getData(@Body UserPostRequestBody request)
I get error message
我收到错误信息
retrofit.RetrofitError: apiCall: Non-body HTTP method cannot contain @Body or @TypedOutput.
Have you any idea?
你有什么想法吗?
采纳答案by harshitpthk
To send data along with your Get Request
you can do the following:
要与您一起发送数据,您Get Request
可以执行以下操作:
//sending data as a url parameter
@GET("/group/{id}/users")
List<User> groupList(@Path("id") int groupId);
as said in this SO answer, Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request- Roy Fielding.
正如此SO answer 中所述,GET 的服务器语义受到限制,因此主体(如果有)对请求 Roy Fielding 没有语义意义。
回答by Guybrush
The definition of GET as explained herein this answer explains that a body isn't supposed to mean anything, so Retrofit doesn't allow you to add a body. That said, it is not true that a server HAS to follow this format. It's entirely possible for a server to have a GET endpoint that not only takes, but REQUIRES a body. It would be "bad" architecture, but it seems a bit silly for Retrofit to limit what the library can do unnecessarily.
GET作为解释的定义在这里在这个答案解释说,身体是不应该意味着什么,所以改造不允许添加的身体。也就是说,服务器必须遵循这种格式是不正确的。服务器完全有可能拥有一个不仅需要而且需要一个主体的 GET 端点。这将是“糟糕”的架构,但对于 Retrofit 来说,限制库可以做的不必要的事情似乎有点愚蠢。
Furthermore, the various HTTP methods have different definitions for what they are SUPPOSED to do. For example a GET gets information, a POST creates a new entry/object by providing information to the server, a PUT updates an existing entry/object etc. The problem is, the easiest way to pass complex data to the server, especially when using retrofit, is by using JSON. So the ideal way to GET information from the server while providing a complex filter would be to send a JSON body along with the GET request. Unfortunately, there is no HTTP request method that allows for this under the spec.
此外,各种 HTTP 方法对于它们应该做什么有不同的定义。例如,GET 获取信息,POST 通过向服务器提供信息来创建新条目/对象,PUT 更新现有条目/对象等。问题是,将复杂数据传递给服务器的最简单方法,尤其是在使用改造,是通过使用 JSON。因此,在提供复杂过滤器的同时从服务器获取信息的理想方法是将 JSON 正文与 GET 请求一起发送。不幸的是,在规范下没有允许这样做的 HTTP 请求方法。
回答by satyender
This error also occurs when API required @POST and you are using @GET
当 API 需要 @POST 并且您正在使用 @GET 时也会发生此错误
回答by eriknyk
回答by ragar90
If the request body you need to send is not to complex you can also specify the query parametters as https://myserverdomain.com?somebody[property1]=value
specifically on retrofit you define it like this:
如果您需要发送的请求正文不复杂,您还可以指定查询参数,https://myserverdomain.com?somebody[property1]=value
特别是在改造时,您可以像这样定义它:
Observable<Response<SomeClass>> someMethod(@Query("somebody[property1]") int property1);
that will work at least if you are requesting to a rails server hope this answer helps somebody
至少在您向 Rails 服务器请求时会起作用,希望这个答案对某人有所帮助