開題:在此默認各位看官對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);