当前位置:网站首页>About the handling of variable parameters in the Retrofit network request URL
About the handling of variable parameters in the Retrofit network request URL
2022-08-09 13:16:00 【Xie Dong_】
开题:By default, the judges are right hereRetrofit、以及OkhttpAlready have some understanding and application,So today we won't talk about the basics of getting started,Today we are talkingRetrofitIn the request interface management classURLThe parameter contains the handling of dynamic parameters.一般我们使用Retrofit大部分场景中URLThey are all statically declared with annotations,即URL及pathThe paths are all fixed,The variable part is passed in as an argument to the method,There are some special cases that will require us to use it [email protected]()、或者@POST()的时候URLThe path contains variable parameters,需要动态处理,Below, I will explain to you one by one through examples.
说明:以下所有Retrofit请求的BaseURL为https://192.168.1.101/api/,The interface address is for local testing,It does not mean that the following interfaces are actually available
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
分析:2018for the dynamic variable part,代表指定idMovie,api/MovieList/{movieId}
@GET("MovieList{movieId}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId );
或者
https://192.168.1.101/api/MovieList/2018/comedy
分析:The request specifies the type under the yearcomedy的电影,The variable part is the year/类型 The variable part of the request address is classified as api/{movieId}/{type}
@GET("MovieList{movieId}/{type}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId ,@Path("type") String type);
3.)可变参数在URLafter the question mark
https://192.168.1.101/api/MovieList?movieId=10011
分析:The parameters after the question mark can be used [email protected] are passed in as method parameters
@GET("MovieList")
Observable<ResultEntity<MovieEntity>> getMovieList(@Query("movieId") String movieId);
4.) There are multiple parameters after the question mark :
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.)There are multiple parameters after the question mark,And the number of parameters is not fixed
https://192.168.1.101/api/MovieList?movieId=10011&type=4&year=2013&......
分析:作为Get请求,The following parameters determine the number of parameters according to the specific business,That is, the number of parameters is variable,But not sure how many,可以借助@Querymap
@GET("MovieList")
Observable<ResultEntity<MovieEntity>> getMovieList(@QueryMap Map<String ,Object> map);
2.POST请求
1.) urlcontains variable parameters,post的数据只有一个type
https://192.168.1.101/api/MovieList/2018
分析:url中2018for variable content,postThere is only one parameter that needs to be submittedtype,2018可动态改变
@FormUrlEncoded
@POST("MovieList/{movieId}")
Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId, @Field("type") String type);
2.) urlcontains variable parameters、It needs to be added after the question marktoken,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.) urlcontains variable parameters、It needs to be added after the question marktoken,postThe data is an object(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);
边栏推荐
- Flutter入门进阶之旅(三)Text Widgets
- 1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint
- C# Get system installed .NET version
- 罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
- WeChat payment development process
- 又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
- 智驾科技完成C1轮融资,此前2轮已融4.5亿元
- Intranet penetration tool ngrok usage tutorial
- 全面了解什么是TPS、QPS以及两者的区别
- Adalvo acquires its first branded product, Onsolis
猜你喜欢
8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
K个结点的组内逆序调整
GRPC整体学习
Fragment中嵌套ViewPager数据空白页异常问题分析
HAproxy:负载均衡
shell脚本------函数的格式,传参,变量,递归,数组
Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
LeetCode #101. 对称二叉树
WeChat side: what is consistent hashing, usage scenarios, and what problems does it solve?
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
随机推荐
MySQL 原理与优化,Group By 优化 技巧
26、管道参数替换命令xargs
Shell正则表达式,三剑客之grep命令
已解决IndentationError: unindent does not match any oute r indentation Level
智驾科技完成C1轮融资,此前2轮已融4.5亿元
随机快排时间复杂度是N平方?
十分钟教会你如何使用VitePress搭建及部署个人博客站点
How to upload local file trial version in binary mode in ABAP report
The new features of ABP 6.0.0 - rc. 1
微服务架构的核心关键点
Intranet penetration tool ngrok usage tutorial
redis库没法引入
使用注解将EventBus封装抽取到基类
The grep command Shell regular expressions, the three musketeers
系统提供的堆 VS 手动改写堆
Programmer's Exclusive Romance - Use 3D Engine to Realize Fireworks in 5 Minutes
荣耀携手Blue Yonder,加快企业战略增长
WebView注入Js代码实现大图自适应屏幕点击图片预览详情
WeChat Mini Program Payment and Refund Overall Process
【无标题】