简体   繁体   English

如何使用原始主体 json 发送 GET 请求 - retrofit2

[英]How to send GET request with raw body json - retrofit2

I have a web service with GET method and raw body json like this:我有一个 web 服务,带有GET方法和原始主体 json,如下所示:

在此处输入图像描述

but my function show error:但我的 function 显示错误:

@GET("/api/Sliders")
Call<SliderModel> Sliders(@Field("Page") String Page,
                          @Field("Count") String Count,
                          @Field("Cat") String Cat);

my error:我的错误:

 @Field parameters can only be used with form encoding. (parameter #1)

Check this out and let me know if this works for you, a while ago I worked with custom verbs and this may be helpful.看看这个,让我知道这是否适合你,不久前我使用自定义动词,这可能会有所帮助。

interface ApiService {
    @HTTP(method = "GET", path = "/path", hasBody = true)
    fun get(body: RequestBody)
}

When calling your API pass RequestBody:调用您的 API 时通过 RequestBody:

RequestBody.create(MediaType.parse("media-type"), "content")

Media Types媒体类型


By the way, It's better to switch to a POST or Custom Verb.顺便说一句,最好切换到 POST 或自定义动词。 Using GET is not a good idea.使用 GET 不是一个好主意。

Follow this按照这个

First Create a Java Class SliderModel.java首先创建一个 Java Class SliderModel.java

在此处输入图像描述

after that do like this.之后这样做。

在此处输入图像描述

public interface ApiInterface {

    @GET("/api/Sliders")
    Call<SliderModel> getPosts();


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM