当前位置:网站首页>关于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);
边栏推荐
- GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
- ABAP 报表中如何以二进制方式上传本地文件试读版
- [Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
- MySQL查询性能优化七种武器之索引潜水
- 二叉树的序列化和反序列化
- 1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
- World's 4th mad scientist dies on his 103rd birthday
- API调用,API传参,面向对接开发,你真的会写接口文档吗?
- Double pointer - the role of char **, int **
- Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
猜你喜欢
Reading and writing after separation, performance were up 100%
Say goodbye to the AI era of hand looms
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
00后写个暑假作业,被监控成这笔样
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
用 API Factory 产品生成 API 文档
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
随机推荐
shell脚本------函数的格式,传参,变量,递归,数组
How to upload local file trial version in binary mode in ABAP report
报告:想学AI的学生数量已涨200%,老师都不够用了
数字化转型之支撑保障单元
Manchester city launch emotional intelligence scarf can be detected, give the fans
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
阿里云新增三大高性能计算解决方案,助力生命科学行业快速发展
redis库没法引入
Say goodbye to the AI era of hand looms
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
goalng-sync/atomic原子操作
WeChat Mini Program Payment and Refund Overall Process
在北极都可以穿短袖了,温度飙升至32.5℃
1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
HAproxy:负载均衡
Blazor Server (9) from scratch -- modify Layout
Recommend a free 50-hour AI computing platform
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers