当前位置:网站首页>关于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);
边栏推荐
- 2022牛客多校(六)M. Z-Game on grid
- electron 应用开发优秀实践
- 放下手机吧:实验表明花20分钟思考和上网冲浪同样快乐
- ansible-cmdb友好展示ansible收集主机信息
- 【小程序】低代码+小游戏=小游戏可视化开发
- Here comes the question: Can I successfully apply for 8G memory on a machine with 4GB physical memory?
- Two minutes recording can pass by second language!The volcano how to practice and become voice tone reproduction technology?
- MySQL principle and optimization of Group By optimization techniques
- win10编译x264库(也有生成好的lib文件)
- 数据挖掘-06
猜你喜欢

ThreadLocal的简单理解

非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer

金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...

Manchester city launch emotional intelligence scarf can be detected, give the fans

Too much volume... Tencent was asked on the side that the memory was full, what would happen?

箭头函数和普通函数的常见区别

C# Get system installed .NET version

已解决IndentationError: unindent does not match any oute r indentation Level

你没见过的《老友记》镜头,AI给补出来了|ECCV 2022

【Untitled】
随机推荐
发明时代,「幂集创新」事关你我
曼城推出可检测情绪的智能围巾,把球迷给整迷惑了
阻塞、非阻塞、多路复用、同步、异步、BIO、NIO、AIO 一锅端
8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
【Untitled】
非科班AI小哥火了:他没有ML学位,却拿到DeepMind的offer
WeChat Mini Program Payment and Refund Overall Process
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
Rust从入门到精通04-数据类型
Say goodbye to the AI era of hand looms
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
西湖大学教授怎么看AI制药革命?|量子位智库圆桌实录
Scala 高阶(七):集合内容汇总(上篇)
Intranet penetration tool ngrok usage tutorial
我们真的需要DApp吗?App真的不能满足我们的幻想吗?
World's 4th mad scientist dies on his 103rd birthday
The grep command Shell regular expressions, the three musketeers
MySQL 原理与优化,Group By 优化 技巧
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
听声辨物,这是AI视觉该干的???|ECCV 2022