对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请求)

http://102.10.10.132/api/News

    @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/{commentId}

    @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/{commentId}?access_token={access_token}

    @DELETE("Comments/{commentId}")
Call<ResponseBody> deleteNewsCommentFromAccount(
@Path("commentId") String commentId,
@Query("access_token") String access_token);

样式3(带有body)

http://102.10.10.132/api/Comments

@HTTP(method = "DELETE",path = "Comments",hasBody = true)
Call<ResponseBody> deleteCommont(
@Body CommentBody body
);

CommentBody:需要提交的内容,与Post中的Body相同

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

  • Tips1
    使用@Field时记得添加@FormUrlEncoded
  • Tips2
    若需要重新定义接口地址,可以使用@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);

希望对大家有帮助,打赏什么的就不用了

作者:带心情去旅行
链接:https://www.jianshu.com/p/7687365aa946
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...(转)的更多相关文章

  1. 【Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap.

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

  2. Android Retrofit网络请求Service,@Path、@Query、@QueryMap、@FieldMap (转)

    GET请求 多个参数在URL问号之后,且个数不确定 http://api.stay4it.com/News?newsId=1&type=类型1- http://api.stay4it.com/ ...

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

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

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

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

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

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

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

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

  7. android Observable api请求参数设置注解问题

    android Observable api请求参数设置注解问题 2018-10-29 20:05:24.919 11786-11786/xxx E/wxh: getQuote=USD getBase ...

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

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

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

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

随机推荐

  1. JAVA学习笔记--ClassLoader

    仅先摘要书中内容以记之,后续也许需要更深入的去探索.先推荐篇博文http://blog.csdn.net/xyang81/article/details/7292380 6.9 初始化和类装载 在许多 ...

  2. spring boot和邮件服务

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址

  3. Oracle DB 查看预警日志

    “Database(数据库)”主页>“Related Links相关链接)”区域> “Alert Log Content (预警日志内容)” 查看预警日志每个数据库都有一个alert_&l ...

  4. HTTP请求时候总是设置的两个参数ConnectionTimeOut和SocketTimeOut

    在HTTP请求时候总是设置两个参数,就是连接超时和Socket超时 public static final String SO_TIMEOUT = "http.socket.timeout& ...

  5. centos7中yum安装lamp环境

    一.准备工作 1.1 环境 操作系统:centos7(CentOS-7-x86_64-Minimal-1708) 硬件:(这个根据项目运行和配置建议设置,一般我先配个1核1G) 1.2 关闭selin ...

  6. HDU 6024 Building Shops (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 题意:有n个room在一条直线上,需要这这些room里面建造商店,如果第i个room建造,则要总 ...

  7. centos 7 环境准备工作

    删除自带jdk: rpm -e --nodeps `rpm -qa | grep java` 打开 /etc/ 目录下的 profile 文件: vi /etc/profile 将如下代码追加到 pr ...

  8. DJango安装-windows

    1.进入虚拟环境后启动 activate 2.查看当前虚拟环境是否存在Django环境 pip list 3.不存在则 安装Django环境 pip install django 4.查看Django ...

  9. Git010--解决冲突

    Git--解决冲突 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  10. GD Library extension not available

    在后台文章上传封面时,遇到了这样一个错误 GD Library extension not available with this PHP installation Ubuntu Nginx 自己在本 ...