使用 Retrofit 的批量请求

Posted

技术标签:

【中文标题】使用 Retrofit 的批量请求【英文标题】:Batch request using Retrofit 【发布时间】:2014-12-04 23:57:21 【问题描述】:

我想使用 Retrofit 执行批处理请求。它有什么好的方法,如何实现呢?基本上我要做的是替换 URL 的查询部分中的一些字符(仅在 URL 的路径部分中允许替换块 - 使用 @Path 注释)。

这是我的问题的伪代码。

@GET("/v2/multi?requests=/users/self,/venues/search?client_id=client_id&client_secret=client_secret&v=v&ll=ll&intent=intent&limit=limit")
    ProfileSearchVenuesResponse searchVenuesAndProfiles(@ReplaceBy("client_id") String clientId,
                          @ReplaceBy("client_secret") String clientSecret,
                          @ReplaceBy("v") int version,
                          @ReplaceBy("ll") String location,
                          @ReplaceBy("intent") String intent,
                          @ReplaceBy("limit") int limit);

【问题讨论】:

【参考方案1】:

@Query 就是你要找的东西:

@GET("/v2/multi?requests=/users/self,/venues/search")
ProfileSearchVenuesResponse searchVenuesAndProfiles(
    @Query("client_id") String clientId,
    @Query("client_secret") String clientSecret,
    @Query("v") int version,
    @Query("ll") String location,
    @Query("intent") String intent,
    @Query("limit") int limit);

在 Retrofit 的 1.7.0 版(昨天发布)中,在原始问题中尝试使用 @Path 的异常消息会指导您正确的解决方案:

URL 查询字符串“client_id=client_id&client_secret=client_secret&v=v&ll=ll&intent=intent&limit=limit”不能有替换块。对于动态查询参数,请使用@Query。

【讨论】:

谢谢!我使用的是 1.6.0,所以我没有得到可以使用 @Query 的信息。

以上是关于使用 Retrofit 的批量请求的主要内容,如果未能解决你的问题,请参考以下文章

Android Retrofit的基本使用

Android Retrofit的基本使用

Android Retrofit的基本使用

RxJava + Retrofit完成网络请求

网络请求--Retrofit2用法

Andorid基础知识——Retrofit的基本使用