开题:在此默认各位看官对Retrofit、以及Okhttp已经有过必定的了解及应用,因此今天咱们不谈基础入门的东西,今天咱们谈在Retrofit请求接口管理类中URL参数含有动态参数的处理方式。通常咱们使用Retrofit大部分场景中URL都是以注解的方式静态声明的,即URL及path路径都是固定不变,可变部分做为方法的参数传入,那有一些特殊状况会要求咱们再使用@GET()、或者@POST()的时候URL路径里含有可变参数,须要动态处理,下面经过例子我逐个为你们分析讲解。java
说明:如下全部Retrofit请求的BaseURL为https://192.168.1.101/api/,接口地址为本地测试,不代码如下接口真实可用json
1.GET请求 api
1.)普通get请求服务器
https://192.168.1.101/api/MovieListsession
@GET("MovieList") Observable<ResultEntity<MovieEntity>> getMovieList();
2.) url中含有参数post
https://192.168.1.101/api/MovieList/2018 测试
分析:2018为动态可变部分,表明指定idMovie,api/MovieList/{movieId}ui
@GET("MovieList{movieId}") Observable<ResultEntity<MovieEntity>> getMovieList(@Path("movieId") String movieId );
或者url
https://192.168.1.101/api/MovieList/2018/comedy spa
分析:请求指定年下类型为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);
以上内容 转自:https://blog.csdn.net/xieluoxixi/article/details/80092582
另外还有几点
1.若是你的可变参数中是带斜杠“/”的,好比https://192.168.1.101/api/MovieList/session/token,
session和token都是可变参数,但session是已知的,只是可能不一样的请求下要求变为不一样的字段,如
https://192.168.1.101/api/MovieList/apiKey/token,而baseURL始终为https://192.168.1.101/api/MovieList/
@POST("session/{movieId}") Call<ResponseBody> getSessionKey(@Path(value = "movieId", encoded = true) String movieId, @Body RequestBody req);
2.若是你须要用到delete请求,好比
@DELETE("event/{uuid}") Observable<ResponseBody> delEvent(@Path(value = "uuid", encoded = true) String uuid, @Body RequestBody rb);
直接这样用就会报错java.lang.IllegalArgumentException:Non-body HTTP method cannot contain @Body
听说官网表示DELETE并不支持向服务器传body
必须更换一下写法:
@HTTP(method = "DELETE",path = "event/{uuid}",hasBody = true) Observable<ResponseBody> delEvent(@Path(value = "uuid", encoded = true) String uuid, @Body RequestBody rb);