前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android】Retrofit网络请求参数注解,@Path、@Query、@QueryMap...

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

作者头像
Gavin-ZYX
发布2018-05-18 15:13:25
1.6K0
发布2018-05-18 15:13:25
举报

对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了。

下面分为GET、POST、DELETE还有PUT的请求,说明@Path、@Query、@QueryMap、@Body、@Field的用法。

初始化Retrofit
代码语言:javascript
复制
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

代码语言:javascript
复制
    @GET("News")
    Call<NewsBean> getItem();
样式2(URL中有参数)

http://102.10.10.132/api/News/1 http://102.10.10.132/api/News/{资讯id}

代码语言:javascript
复制
    @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}/{类型}

代码语言:javascript
复制
    @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}

代码语言:javascript
复制
    @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={类型}

代码语言:javascript
复制
    @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={类型}...

代码语言:javascript
复制
    @GET("News")
    Call<NewsBean> getItem(@QueryMap Map<String, String> map);

也可以

代码语言:javascript
复制
    @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}

代码语言:javascript
复制
    @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}

代码语言:javascript
复制
    @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}

代码语言:javascript
复制
    @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}

代码语言:javascript
复制
    @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}

代码语言:javascript
复制
    @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

代码语言:javascript
复制
@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}

代码语言:javascript
复制
    @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,将地址以参数的形式传入即可。如
代码语言:javascript
复制
    @GET
    Call<List<Activity>> getActivityList(
            @Url String url,
            @QueryMap Map<String, String> map);
代码语言:javascript
复制
    Call<List<Activity>> call = service.getActivityList(
                "http://115.159.198.162:3001/api/ActivitySubjects", map);
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.04.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • GET
    • 样式1(一个简单的get请求)
      • 样式2(URL中有参数)
        • 样式3(参数在URL问号之后)
          • 样式4(多个参数在URL问号之后,且个数不确定)
            • POST
              • 样式1(需要补全URL,post的数据只有一条reason)
              • 样式2(需要补全URL,问号后加入access_token,post的数据只有一条reason)
              • 样式3(需要补全URL,问号后加入access_token,post一个body(对象))
              • 样式1(需要补全URL)
              • 样式2(需要补全URL,问号后加入access_token)
              • 样式3(带有body)
          • DELETE
          • PUT(这个请求很少用到,例子就写一个)
          • 总结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档