当前位置:网站首页>关于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);
边栏推荐
- 基于STM32+铂电阻设计的测温仪
- Apexsqlrecover cannot connect to database
- 26、管道参数替换命令xargs
- ARP协议原理
- Web console control edit box
- AQS同步组件-FutureTask解析和用例
- Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
- 张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
- 合并两个有序列表
- Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
猜你喜欢
读写分离后,性能居然提升100%了呀
内网穿透工具ngrok使用教程
Double pointer - the role of char **, int **
AQS Synchronization Component - FutureTask Analysis and Use Cases
HAproxy:负载均衡
World's 4th mad scientist dies on his 103rd birthday
GPT-3组合DALL·E,60秒内搞定游戏设定和原型动画!网友看后:这游戏想玩
MySQL 原理与优化,Group By 优化 技巧
智驾科技完成C1轮融资,此前2轮已融4.5亿元
Resolved IndentationError: unindent does not match any oute r indentation Level
随机推荐
数据挖掘-06
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
正则表达式(规则,匹配,和实际使用)
How should the acceptance criteria for R&D requirements be written?| Agile Practices
内网穿透工具ngrok使用教程
智驾科技完成C1轮融资,此前2轮已融4.5亿元
redis库没法引入
AQS Synchronization Component - FutureTask Analysis and Use Cases
Shell正则表达式,三剑客之grep命令
FFmpeg compiles and installs on win10 (configure libx264)
Batch大小不一定是2的n次幂!ML资深学者最新结论
World's 4th mad scientist dies on his 103rd birthday
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
ARP协议原理
无需精子卵子子宫体外培育胚胎,Cell论文作者这番话让网友们炸了
PM2 configuration file
Django cannot link mysql database
Apexsqlrecover cannot connect to database
基于CAP组件实现补偿事务与幂等性保障
Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?