GET请求

多个参数在URL问号之后,且个数不确定

http://api.stay4it.com/News?newsId=1&type=类型1… 
http://api.stay4it.com/News?newsId={资讯id}&type={类型}…

@GET("News")
Call<NewsBean> getItem(@QueryMap Map<String, String> map);
  • 1
  • 2

或者:

  @GET("News")
Call<NewsBean> getItem(
@Query("newsId") String newsId,
@QueryMap Map<String, String> map);
  • 1
  • 2
  • 3
  • 4

POST请求

  • 需要补全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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 需要补全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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 需要补全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);
  • 1
  • 2
  • 3
  • 4
  • 5

DELETE

需要补全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("accountId") String accountId);
  • 1
  • 2
  • 3

需要补全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);
  • 1
  • 2
  • 3
  • 4

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);
  • 1
  • 2
  • 3
  • 4
  • 5

总结

@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);
  • 1
  • 2
  • 3
  • 4
Call<List<Activity>> call = service.getActivityList(
"http://115.159.198.162:3001/api/ActivitySubjects", map);
  • 1
  • 2

参考简书:http://www.jianshu.com/p/7687365aa946

还有@FieldMap

如匿名发表新评论:

接口地址为: /posts/create

HTTP请求方式: POST

请求示例为:

Request URL: http://api.duoshuo.com/posts/create.json 
Request Method: POST 
Post Data: short_name=official&author_email=jp.chenyang%40gmail.com&author_name=Perchouli&thread_id=1152923703638301959&author_url=http%3A%2F%2Fduoshuo.com&message=匿名发表新评论

1.Field方式实现

@FormUrlEncoded
@POST("/posts/create.json")
Call<CommitResult> createCommit(@Field("secret") String secret,
@Field("short_name") String shortName,
@Field("author_email") String authorEmail,
@Field("author_name") String authorName,
@Field("thread_key") String threadKey,
@Field("author_url") String author_url,
@Field("message") String message);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.Field Map实现方式

@FormUrlEncoded
@POST("/posts/create.json")
Call<CommitResult> createCommit(@FieldMap Map<String, String> map);
  • 1
  • 2
  • 3

Android Retrofit网络请求Service,@Path、@Query、@QueryMap、@FieldMap (转)的更多相关文章

  1. Android Retrofit网络请求Service,@Path、@Query、@QueryMap、@Map...

    http://blog.csdn.net/jdsjlzx/article/details/51607867

  2. 【Android】Retrofit网络请求Service,@Path、@Query、@QueryMap...

    对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了. 下面分为GET.POST.DELETE还有PUT的请求,说明@Path.@Query.@QueryMap.@Bo ...

  3. Android okHttp网络请求之Retrofit+Okhttp+RxJava组合

    前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少少有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下R ...

  4. 基于Retrofit+RxJava的Android分层网络请求框架

    目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...

  5. Android okHttp网络请求之Json解析

    前言: 前面两篇文章介绍了基于okHttp的post.get请求,以及文件的上传下载,今天主要介绍一下如何和Json解析一起使用?如何才能提高开发效率? okHttp相关文章地址: Android o ...

  6. Android okHttp网络请求之Get/Post请求

    前言: 之前项目中一直使用的Xutils开源框架,从xutils 2.1.5版本使用到最近的xutils 3.0,使用起来也是蛮方便的,只不过最近想着完善一下app中使用的开源框架,由于Xutils里 ...

  7. Android okHttp网络请求之文件上传下载

    前言: 前面介绍了基于okHttp的get.post基本使用(http://www.cnblogs.com/whoislcj/p/5526431.html),今天来实现一下基于okHttp的文件上传. ...

  8. Android okHttp网络请求之缓存控制Cache-Control

    前言: 前面的学习基本上已经可以完成开发需求了,但是在项目中有时会遇到对请求做个缓存,当没网络的时候优先加载本地缓存,基于这个需求我们来学习一直okHttp的Cache-Control. okHttp ...

  9. Retrofit网络请求库应用02——json解析

    PS:上一篇写了Retrofit网络请求库的简单使用,仅仅是获取百度的源码,来证明连接成功,这篇讲解如何解析JSON数据,该框架不再是我们之前自己写的那样用JsonArray等来解析,这些东西,我们都 ...

随机推荐

  1. dedecms调用标签总结(二)

    6. 列表调用 首页: {dede:arclist row='10' titlelen='24' typeid='2' orderby='pubdate'} <li> <a href ...

  2. 【2016-10-11】【坚持学习】【Day2】【代理模式】

    今天学习了代理模式. 定义 官方: 代理模式:给某一个对象提供一个代理或占位符,并由代理对象来控制对原对象的访问. Proxy Pattern: Provide a surrogate or plac ...

  3. vmware虚拟机 32位Ubuntu 安装matlab_R2012A问题小记

    最近由于学校课程的需要,所以在Ubuntu上捣腾了一下matlab,经历曲折,记录一下,希望能让大家少跳点坑.先分享一个有效的下载链接: http://www.matlab.org.cn/Downlo ...

  4. 网格弹簧质点系统模拟(Spring-Mass System by Euler Integration)

    弹簧质点模型是利用牛顿运动定律来模拟物体变形的方法.如下图所示,该模型是一个由m×n个虚拟质点组成的网格,质点之间用无质量的.自然长度不为零的弹簧连接.其连接关系有以下三种: 1.连接质点[i, j] ...

  5. Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]

    C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Svn Patch 中文乱码

    关于Patch svn打patch的介绍:巧用svn create patch(打补丁)方案解决定制版需求 svn创建patch 1.在SVN的提交列表中,右键选择“创建补丁” 2.选择保存位置,保存 ...

  7. 使用 Flash Builder 的 Apple iOS 开发过程

    使用 Flash Builder 的 Apple iOS 开发过程   iOS 开发和部署过程概述 构建.调试或部署 iOS 应用程序前的准备工作 在测试.调试或安装 iOS 应用程序时选择的文件 将 ...

  8. 解决Apache/PHP无法启动的问题

    最近经常被问到Apache无法启动的情况,所以写一篇文章,总结一下Windows下经常遇到的 Apache/PHP 无法启动的情况的解决方法. Apache/PHP 无法启动分两种情况: 1..Apa ...

  9. ADO.Net 增、删、改、查(综合练习)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. codevs[1300]文件排版

    Description 写电子邮件是有趣的,但不幸的是经常写不好看,主要是因为所有的行不一样长,你的上司想要发排版精美的电子邮件,你的任务是为他编写一个电子邮件排版程序. 完成这个任务最简单的办法是在 ...