当前位置:网站首页>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);
边栏推荐
猜你喜欢
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
The latest interview summary in 20022 brought by Ali senior engineer is too fragrant
放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
MySQL 原理与优化,Group By 优化 技巧
GRPC整体学习
两个链表相加
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
问题来了:4GB物理内存的机器上申请8G内存能成功吗?
李开复花上千万投的缝纫机器人,团队出自大疆
2022牛客多校(六)M. Z-Game on grid
随机推荐
Golang学习之路(五):Golang的函数
数据挖掘-06
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
Go-based web access parameters
又有大厂员工连续加班倒下/ 百度搜狗取消快照/ 马斯克生父不为他骄傲...今日更多新鲜事在此...
Flutter入门进阶之旅(四)文本输入Widget TextField
【微服务~远程调用】整合RestTemplate、WebClient、Feign
两分钟录音就可秒变语言通!火山语音音色复刻技术如何修炼而成?
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
Shell之常用小工具(sort、uniq、tr、cut)
ThreadLocal的简单理解
国产抗新冠口服药每瓶不超300元/ 我国IPv6网络全面建成/ 谷歌入局折叠屏手机...今日更多新鲜事在此...
合并两个有序列表
ABAP 面试题:如何使用 ABAP 编程语言的 System CALL 接口,直接执行 ABAP 服务器所在操作系统的 shell 命令?
【Untitled】
JD.com architects tidy up: what are the core technical knowledge points of jvm and performance tuning
听声辨物,这是AI视觉该干的???|ECCV 2022
How should the acceptance criteria for R&D requirements be written?| Agile Practices
We really need DApp?Really can't meet our fantasy App?
WebView注入Js代码实现大图自适应屏幕点击图片预览详情