【Android】Retrofit网络请求Service,@Path、@Query、@QueryMap...
对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了。
下面分为GET、POST、DELETE还有PUT的请求,说明@Path、@Query、@QueryMap、@Body、@Field的用法。
初始化Retrofit
String BASE_URL = "http://102.10.10.132/api/"; //主机地址
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.build();
GET
样式1(一个简单的get请求)
@GET("News")
Call<NewsBean> getItem();
样式2(URL中有参数)
http://102.10.10.132/api/News/1
http://102.10.10.132/api/News/{资讯id}
@GET("News/{newsId}")
Call<NewsBean> getItem(@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<NewsBean> getItem(@Path("newsId") String newsId, @Path("type") String type);
样式3(参数在URL问号之后)
http://102.10.10.132/api/News?newsId=1
http://102.10.10.132/api/News?newsId={资讯id}
@GET("News")
Call<NewsBean> getItem(@Query("newsId") String newsId);
或
http://102.10.10.132/api/News?newsId=1&type=类型1
http://102.10.10.132/api/News?newsId={资讯id}&type={类型}
@GET("News")
Call<NewsBean> getItem(@Query("newsId") String newsId, @Query("type") String type);
样式4(多个参数在URL问号之后,且个数不确定)
http://102.10.10.132/api/News?newsId=1&type=类型1...
http://102.10.10.132/api/News?newsId={资讯id}&type={类型}...
@GET("News")
Call<NewsBean> getItem(@QueryMap Map<String, String> map);
也可以
@GET("News")
Call<NewsBean> getItem(
@Query("newsId") String newsId,
@QueryMap Map<String, String> map);
POST
样式1(需要补全URL,post的数据只有一条reason)
http://102.10.10.132/api/Comments/1
http://102.10.10.132/api/Comments/{newsId}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Field("reason") String reason);
样式2(需要补全URL,问号后加入access_token,post的数据只有一条reason)
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Field("reason") String reason);
样式3(需要补全URL,问号后加入access_token,post一个body(对象))
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Body CommentBean bean);
DELETE
样式1(需要补全URL)
http://102.10.10.132/api/Comments/1
http://102.10.10.132/api/Comments/{newsId}
{access_token}
@DELETE("Comments/{commentId}")
Call<ResponseBody> deleteNewsCommentFromAccount(
@Path("commentId") String commentId);
样式2(需要补全URL,问号后加入access_token)
http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}
@DELETE("Comments/{commentId}")
Call<ResponseBody> deleteNewsCommentFromAccount(
@Path("accountId") String accountId,
@Query("access_token") String access_token);
PUT(这个请求很少用到,例子就写一个)
http://102.10.10.132/api/Accounts/1
http://102.10.10.132/api/Accounts/{accountId}
@PUT("Accounts/{accountId}")
Call<ExtrasBean> updateExtras(
@Path("accountId") String accountId,
@Query("access_token") String access_token,
@Body ExtrasBean bean);
总结
@Path:所有在网址中的参数(URL的问号前面),如:
http://102.10.10.132/api/Accounts/{accountId}
@Query:URL问号后面的参数,如:
http://102.10.10.132/api/Comments?access_token={access_token}
@QueryMap:相当于多个@Query
@Field:用于POST请求,提交单个数据
@Body:相当于多个@Field,以对象的形式提交
TIps
- Tip1
使用@Field时记得添加@FormUrlEncoded - Tip2
若需要重新定义接口地址,可以使用@Url,将地址以参数的形式传入即可。如@GET
Call<List<Activity>> getActivityList(
@Url String url,
@QueryMap Map<String, String> map);Call<List<Activity>> call = service.getActivityList(
"http://115.159.198.162:3001/api/ActivitySubjects", map);
最新文章
- Java for LeetCode 225 Implement Stack using Queues
- swift 2.x学习笔记(二)
- Source Insight 使用
- hdu 2629 Identity Card (字符串解析模拟题)
- android webview 遇到的问题:external/chromium/net/disk_cache/stat_hub.cc:216:
- 欧拉工程第63题:Powerful digit counts
- 用联想wndows8系统做android调试开发,adb server无法启动的原因
- Problem 2214 Knapsack problem 福建第六届省赛
- LeetCode_Permutations
- Spring MVC遭遇checkbox的问题解决方式
- CentOS服务器运维监控Nagios(一)
- 一个简单的div弹出层的小例子
- 怎么看 EOS 的代码最爽?
- IP通信基础学习第四周(下)
- mac 内置PHP配置多站点
- 前端(五)之display 总结与浮动
- spring-AOP之通知和顾问
- 【内核】linux2.6版本内核编译配置选项(二)
- 面面具到!android重力传感器
- lintcode101 删除排序数组中的重复数字 II
热门文章
- 手机端布局,rem布局动态获取根字体大小
- Java之IO(零)总结
- ruby:TypeError: 对象不支持此属性或方法(<;%= stylesheet_link_tag &#39;application&#39;, media: &#39;all&#39;, &#39;data-turbolink)
- OpenGL10-骨骼动画原理篇(3)-Shader版本代码已经上传
- 用sinopia搭建内部npm服务
- 我的zsh 配置
- JAVA 之 多态 抽象 接口
- 软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比
- Linux下最常用的Shell命令的介绍
- (译) 在AngularJS中使用的表单验证功能