【问题标题】:How to set parameter in interface in retrofit request如何在改造请求的界面中设置参数
【发布时间】:2019-10-05 22:09:36
【问题描述】:

有一个改造界面:

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

我如何在请求时提供参数?例如,

/news?id=1001

我想它一定是这样的:

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

但是我该如何正确地做呢?

【问题讨论】:

    标签: java android interface request retrofit


    【解决方案1】:
    @GET("/v1/news_content")
    Call<ItemPageAPI> getAllItems(@Query("id") String id);
    

    @Query 可以自己将参数添加到 URL。

    【讨论】:

      【解决方案2】:

      请这样使用:

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

      如果@Path 注释不起作用,那么您可以传入@Query 注释。

      【讨论】:

        【解决方案3】:

        你可以在改造请求中这样设置参数,headers

          @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>
        

        【讨论】:

          【解决方案4】:
          public interface GetDataService {
             @GET("/news?id={id}")
             Call<ItemAPI> getAllItems(@Query("id") int id);
           }
          

          【讨论】:

            猜你喜欢
            • 2022-10-06
            • 1970-01-01
            • 2019-07-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-15
            • 2014-11-03
            相关资源
            最近更新 更多