关于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);

 

 
 

 

转载于:https://www.cnblogs.com/Sharley/p/10450586.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您使用Retrofit发送POST请求@Body方式处理数据库参数,可以按照以下步骤进行操作: 1. 创建一个Java类,用于存储参数。例如,如果您要向数据库添加一条用户记录,可以创建一个名为User的Java类,其属性包括用户名,密码等等。 ```java public class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public String getPassword() { return password; } } ``` 2. 在Retrofit接口定义POST请求,使用@Body注解将User对象作为请求体发送到服务器。 ```java public interface ApiService { @POST("users") Call<Void> createUser(@Body User user); } ``` 3. 在应用使用Retrofit对象创建接口实例,并调用createUser方法。 ```java // 创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/api/") .addConverterFactory(GsonConverterFactory.create()) .build(); // 创建接口实例 ApiService apiService = retrofit.create(ApiService.class); // 创建User对象 User user = new User("username", "password"); // 发送POST请求 Call<Void> call = apiService.createUser(user); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { // 请求成功 } @Override public void onFailure(Call<Void> call, Throwable t) { // 请求失败 } }); ``` 上述代码将向服务器发送一条POST请求,将User对象作为请求体发送到服务器。如果请求成功,服务器将会将User对象存储到数据库。如果请求失败,将会调用onFailure方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值