简体   繁体   English

如何在改造请求的接口中设置参数

[英]How to set parameter in interface in retrofit request

There is an interface for retrofit: 有一个用于改造的接口:

public interface GetDataService {
    @GET("/news")
    Call<ItemAPI> getAllItems();
}

How I can give parameters when I do the request? 请求时如何给参数? For example, 例如,

/news?id=1001

I think it must be looking like: 我认为它一定看起来像:

@GET("/news?id={id}")

But how do I do it correctly? 但是我该怎么做呢?

@GET("/v1/news_content")
Call<ItemPageAPI> getAllItems(@Query("id") String id);

@Query can add your parameters to the URL by itself. @Query可以自己将参数添加到URL。

Please use it like this: 请像这样使用它:

@GET("/news?id={id}")
Call<ItemAPI> getAllItems(@Path("id") String idStr);

If the @Path annotation is not working then you can pass in the @Query annotation. 如果@Path注释不起作用,则可以传入@Query注释。

public interface GetDataService {
   @GET("/news?id={id}")
   Call<ItemAPI> getAllItems(@Query("id") int id);
 }

You can set parameters, headers in this way in the retrofit requests 您可以通过这种方式在改装请求中设置参数,标题

  @Headers("Accept: " + "application/json")
  @GET(Constants.GET_PROPERTIES)
    fun getFilteredProperties(@Query("access_token") access_token: String,
                              @Query("lat") lat: String,
                              @Query("long") long: String,
                              @Query("current_page") current_page: String,
                              @Query("location_name") location_name: String
    ): Call<GetPropertiesPojo>

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

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