网络请求:Android Retrofit 2.0(一)初次见面请多多关照_艾阳Blog的博客-优快云博客
//@POST 增 @DELETE 删 @PUT 改 @GET 查 //普通表单,key-value
//@Field和@Body区别:
//@Field:用于POST请求,提交单个数据
//@Body:相当于多个@Field,以对象的形式提交
//@GET
//使用@Query 拼接参数查询,获取后台数据,原来 http://192.168.43.173/api/trades?userId={用户id}
//参数在url问号之后 userId要和后台对应一样的
@GET("trades")
Call<resultUser> getItem(@Query("userId") String userId);
//使用@QueryMap 获取后台查询集合 即多个参数组成的Map数组
@GET("/search/users")
Call<resultUser> searchUsers(@QueryMap Map<String, String> params);
//使用@Path URL中有参数
//http://102.10.10.132/api/News/1
//http://102.10.10.132/api/News/{资讯id} //newsId = 资讯id
@GET("News/{newsId}")
Call<resultUser> getPath(@Path("newsId") String newsId);
//或
//http://102.10.10.132/api/News/1/类型1
//http://102.10.10.132/api/News/{资讯id}/{类型}
@GET("News/{newsId}/{type}")
Call<resultUser> getItem(@Path("newsId") String newsId, @Path("type") String type);
//@PoST
//使用@Body 默认转换json 是上传json格式数据 使用@Body意思就是提交多个数据
@POST("users/new")
Call<resultUser> createUser(@Body resultUser user);
//使用 @Field 提交单个数据。 比如注册_验证手机号 要加上@FormUrlEncoded
@FormUrlEncoded
@POST("user/edit")
//first_name要和后台对应一样的
Call<resultUser> updateUser(@Field("first_name") String first, @Field("last_name") String last);
//使用 @FieldMap //普通表单,key-value 键 值 要加上@FormUrlEncoded
/*
*
* Map<String, Object> params = new HashMap<String, Object>();
//普通表单,key-value
params.put("key", "value");
* */
//使用@HeaderMap 如果要动态的定义请求头,动态添加 @HeaderMap 键 值 上传字符串数据
Call<resultUser> weather(@HeaderMap Map<String, String> headers);
//使用 @PartMap 上传字符串,包括用户信息以及反馈文本;
//使用 @Part 上传的图片
//就是反馈用户的产品信息,要求用户可以既能上传图片,又能上传文字。然后跟后台商量好数据传递方式:
@Multipart
@POST("user/edit")
Call<resultUser> compensateCreate(
//map:上传字符串,包括用户信息以及反馈文本;
//parts:上传的图片
@PartMap Map<String, String> map, @Part List<MultipartBody.Part> parts);
//使用 @Part 可以提交多种类型,比如文本和图片上传到后台
@Multipart
@PUT("user/photo")
Call<resultUser> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);
--------------------------------------------------------------------------------------------------------------
大神详情网站:Retrofit网络请求参数注解,@Path、@Query、@Post、Body等总结_郭_昊的博客-优快云博客_@post
Retrofit网络请求参数注解,@Path、@Query、@Post、Body等总结
具体用法参照 Retrofit官网
Retrofit简介:
是一个基于okhttp的网络请求框架
通过注解配置网络请求参数
图片链接和图片上传
支持同步和异步网络请求
支持多种数据的解析,提供对Rxjava的支持
可拓展性好,高度封装,简洁易用
Retrofit使用介绍:
使用 Retrofit 的步骤共有7个:
1
- 添加Retrofit库的依赖
- 创建接收服务器返回数据的类
- 创建用于描述网络请求的接口
- 创建 Retrofit 实例
- 创建 网络请求接口实例 并 配置网络请求参数
- 发送网络请求(异步 / 同步)
- 处理数据
GET
http://192.168.43.173/api/trades
//简单的get请求(没有参数)
@GET("trades")
Call<TradesBean> getItem();
http://192.168.43.173/api/trades/{userId}
//简单的get请求(URL中带有参数)
@GET("News/{userId}")
Call<TradesBean> getItem(@Path("userId") String userId);
//简单的get请求(URL中带有两个参数)
@GET("News/{userId}")
Call<TradesBean> getItem(@Path("userId") String userId,@Path("type") String type);
http://192.168.43.173/api/trades?userId={用户id}
//参数在url问号之后
@GET("trades")
Call<TradesBean> getItem(@Query("userId") String userId);
http://192.168.43.173/api/trades?userId={用户id}&type={类型}
@GET("trades")
Call<TradesBean> getItem(@QueryMap Map<String, String> map);
1
2
@GET("trades")
Call<TradesBean> getItem(
@Query("userId") String userId,
@QueryMap Map<String, String> map);
POST
http://192.168.43.173/api/trades/{userId}
//需要补全URL,post的数据只有一条reason
@FormUrlEncoded
@POST("trades/{userId}")
Call<TradesBean> postResult(
@Path("userId") String userId,
@Field("reason") String reason;
http://192.168.43.173/api/trades/{userId}?token={token}
//需要补全URL,问号后需要加token,post的数据只有一条reason
@FormUrlEncoded
@POST("trades/{userId}")
Call<TradesBean> postResult(
@Path("userId") String userId,
@Query("token") String token,
@Field("reason") String reason;
//post一个对象
@POST("trades/{userId}")
Call<TradesBean> postResult(
@Path("userId") String userId,
@Query("token") String token,
@Body TradesBean bean;
//用不同注解post一个实体
@POST("trades/{userId}")
Call<TradesBean> postResult(
@Part("entity") TradesBean bean;
PUT
//put一个实体
@PUT("trade/carInfo/{pid}")
Call<TradesBean> putInfo(
@Path("pid") Int pid,
@Body CarInfoBean carInfoBean;)
DELETE
http://192.168.43.173/api/trades/{userId}
//补全url
@DELETE("trades/{userId}")
Call<TradesBean> deleteInfo(
@Path("userId") String userId;
http://192.168.43.173/api/trades/{userId}?token={token}
//补全url并且后面还token
@DELETE("trades/{userId}")
Call<TradesBean> deleteInfo(
@Path("userId") String userId,
@Query("token") String token;)
个人总结
Path是网址中的参数,例如:trades/{userId}
Query是问号后面的参数,例如:trades/{userId}?token={token}
QueryMap 相当于多个@Query
Field用于Post请求,提交单个数据,然后要加@FormUrlEncoded
Body相当于多个@Field,以对象的方式提交
@Streaming:用于下载大文件
@Header,@Headers、加请求头