当前位置:网站首页>关于Retrofit网络请求URL中含有可变参数的处理
关于Retrofit网络请求URL中含有可变参数的处理
2022-08-09 12:04:00 【谢栋_】
开题:在此默认各位看官对Retrofit、以及Okhttp已经有过一定的了解及应用,所以今天我们不谈基础入门的东西,今天我们谈在Retrofit请求接口管理类中URL参数含有动态参数的处理方式。一般我们使用Retrofit大部分场景中URL都是以注解的方式静态声明的,即URL及path路径都是固定不变,可变部分作为方法的参数传入,那有一些特殊情况会要求我们再使用@GET()、或者@POST()的时候URL路径里含有可变参数,需要动态处理,下面通过例子我逐个为大家分析讲解。
说明:以下所有Retrofit请求的BaseURL为https://192.168.1.101/api/,接口地址为本地测试,不代表以下接口真实可用
1.GET请求
1.)普通get请求
https://192.168.1.101/api/MovieList
@GET("MovieList")
Observable<ResultEntity<MovieEntity>> getMovieList();
2.) url中含有参数
https://192.168.1.101/api/MovieList/2018
分析:2018为动态可变部分,代表指定idMovie,api/MovieList/{movieId}
@GET("MovieList{movieId}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId );
或者
https://192.168.1.101/api/MovieList/2018/comedy
分析:请求指定年下类型为comedy的电影,可变部分为年份/类型 请求地址可变部分归类为 api/{movieId}/{type}
@GET("MovieList{movieId}/{type}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId ,@Path("type") String type);
3.)可变参数在URL的问号之后
https://192.168.1.101/api/MovieList?movieId=10011
分析:问号之后的参数可以直接用@Query注解在作为方法参数传入
@GET("MovieList")
Observable<ResultEntity<MovieEntity>> getMovieList(@Query("movieId") String movieId);
4.) 问号后面有多个参数 :
https://192.168.1.101/api/MovieList?movieId=10011&type=3
@GET("MovieList")
Observable<ResultEntity<MovieEntity>> getMovieList(@Query("movieId") String movieId,@Query("type") int type);
5.)问号后面有多个参数,且参数个数不定
https://192.168.1.101/api/MovieList?movieId=10011&type=4&year=2013&......
分析:作为Get请求,后面参数根据具体业务确定参数多少,也就是参数个数可变,但不确定多少个,可以借助@Querymap
@GET("MovieList")
Observable<ResultEntity<MovieEntity>> getMovieList(@QueryMap Map<String ,Object> map);
2.POST请求
1.) url中含有可变参数,post的数据只有一个type
https://192.168.1.101/api/MovieList/2018
分析:url中2018为可变内容,post需要提交的参数只有一个type,2018可动态改变
@FormUrlEncoded
@POST("MovieList/{movieId}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId, @Field("type") String type);
2.) url中含有可变参数、问号之后需要加入token,post的数据只有一个type
https://192.168.1.101/api/MovieList/2018?token=4654551321563132fasd5645ds3
@FormUrlEncoded
@POST("MovieList/{movieId}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId,
@Query("token") String token,
@Field("type") String type);
3.) url中含有可变参数、问号之后需要加入token,post的数据为一个对象(json串)
https://192.168.1.101/api/MovieList/2018?token=4654551321563132fasd5645ds3
@POST("MovieList/{movieId}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId,
@Query("token") String token,
@Body MovieEntity entity);
边栏推荐
- 问题来了:4GB物理内存的机器上申请8G内存能成功吗?
- ansible-cmdb友好展示ansible收集主机信息
- WeChat payment development process
- Adalvo收购其首个品牌产品Onsolis
- Go-based web access parameters
- 2022 Niu Ke Duo School (6) M. Z-Game on grid
- Reading and writing after separation, performance were up 100%
- 虚拟机安装出现的问题汇总
- [Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
- 曲鸟全栈UI自动化教学(八):框架代码讲解和进一步优化
猜你喜欢
Win10 compiles the x264 library (there are also generated lib files)
注释、关键字、标识符的区别你知道吗?
阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
26、管道参数替换命令xargs
#物联网征文#小熊派设备开发实战
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
2022 Niu Ke Duo School (6) M. Z-Game on grid
腾讯欲成育碧最大股东/ 米哈游招NLP内容生成研究员/ AI发现四千余物种濒临灭绝...今日更多新鲜事在此...
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
随机推荐
How to upload local file trial version in binary mode in ABAP report
软件测试——金融测试类面试题,看完直接去面试了
How should the acceptance criteria for R&D requirements be written?| Agile Practices
Flutter入门进阶之旅(十)Dialog&Toast
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
PM2 configuration file
微信小程序支付及退款整体流程
报告:想学AI的学生数量已涨200%,老师都不够用了
水能自发变成“消毒水”,83岁斯坦福教授:揭示冬天容易得流感的部分原因...
win10编译x264库(也有生成好的lib文件)
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
26、管道参数替换命令xargs
FFmpeg compiles and installs on win10 (configure libx264)
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
已解决IndentationError: unindent does not match any oute r indentation Level
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
京东架构师呕心整理:jvm与性能调优有哪些核心技术知识点
Recommend a free 50-hour AI computing platform