在一些 POST 请求中,我不知道何时将 @Field 与 FormUrlEncoded 一起使用以及何时使用 @Query
例如:
@POST("list-products-for-sale")
Call<ListAllProductsResponse> getNewProducts(@HeaderMap Map<String,
String> headers,@Query("lastProductId") String lastProductId);
当我在这里尝试使用@Field 时,它没有正确响应,当我将它切换到@Query 时,它工作得很好。
我想知道为什么@Field 不能正常工作,而 Query 可以正常工作,并且我在 POSTMAN 中进行了测试,在那里我将数据作为 formurlencoded 发送,它给了我结果。
编辑
顺便说一句,我正在传递 Content-Type:application/json, Accept: application/json with an Authorization key
最佳答案
@Field
用于发送@FormUrlEncoded
请求 Retrofit
它隐藏了您的参数并且不附加 url
提供安全性。用于POST
要求。@Query
附加到 URL 的参数。
如果您使用 @Field
请求比它会隐藏您的参数,而不是附加网址。
如果您使用 @Query
请求比您的所有参数都附加到您的请求并且对用户可见。
根据您的 api 请求,您必须使用上述注释之一。如果 api 请求接受 @FormUrlEncoded
数据比使用 @Field
或者如果他们想用 url 附加它而不是使用 @Query
.
您可以从以下链接获取更多信息:
1) https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
2) https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/Field.html
关于android - 改造中@Field和@Query有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848058/