当前位置:网站首页>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);
边栏推荐
- 链表噩梦之一?5000多字带你弄清它的来龙去脉
- The FFmpeg library is configured and used on win10 (libx264 is not configured)
- #Internet of Things essay#Xiaoxiong pie equipment development actual combat
- Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
- Apexsqlrecover cannot connect to database
- Flutter入门进阶之旅(四)文本输入Widget TextField
- 罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
- Information system project managers must memorize the core test sites (63) The main process of project portfolio management & DIPP analysis
- 基于STM32+铂电阻设计的测温仪
- Say goodbye to the AI era of hand looms
猜你喜欢
Shell正则表达式,三剑客之grep命令
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
Nature:猪死亡1小时后,器官再次运转
Scala 高阶(七):集合内容汇总(上篇)
无重复字符的最长子串
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
[Microservice ~ Remote Call] Integrate RestTemplate, WebClient, Feign
太卷了... 腾讯一面被问到内存满了,会发生什么?
HAproxy: load balancing
随机推荐
HAproxy:负载均衡
How to upload local file trial version in binary mode in ABAP report
微信一面:一致性哈希是什么,使用场景,解决了什么问题?
C# Get system installed .NET version
微信支付开发流程
ansible-cmdb友好展示ansible收集主机信息
使用RecyclerView实现三级折叠列表
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
Say goodbye to the AI era of hand looms
Scala 高阶(七):集合内容汇总(上篇)
Flutter入门进阶之旅(二)Hello Flutter
LeetCode #101. 对称二叉树
Reading and writing after separation, performance were up 100%
Flutter入门进阶之旅(五)Image Widget
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
数字化转型之支撑保障单元
关于Retrofit网络请求URL中含有可变参数的处理
We really need DApp?Really can't meet our fantasy App?