关于Retrofit网络请求URL中含有可变参数的处理

 开题:在此默认各位看官对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);

 以上内容 转自: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);

 


 

posted @ 2019-02-28 15:16  Sharley  阅读(3782)  评论(0编辑  收藏  举报