当前位置:网站首页>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);

 

 

 

 

 

 

       

 

原网站

版权声明
本文为[Xie Dong_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091203557546.html