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

GET请求

多个参数在URL问号以后,且个数不肯定css

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

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

或者:sql

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

POST请求

  • 须要补全URL,post的数据只有一条reason

http://102.10.10.132/api/Comments/1 
http://102.10.10.132/api/Comments/{newsId}json

 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
@FormUrlEncoded @POST("Comments/{newsId}") Call<Comment> reportComment( @Path("newsId") String commentId, @Field("reason") String reason);
  • 须要补全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}api

 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
@FormUrlEncoded @POST("Comments/{newsId}") Call<Comment> reportComment( @Path("newsId") String commentId, @Query("access_token") String access_token, @Field("reason") String reason);
  • 须要补全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}ruby

 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
@POST("Comments/{newsId}") Call<Comment> reportComment( @Path("newsId") String commentId, @Query("access_token") String access_token, @Body CommentBean bean);

DELETE

须要补全URLpost

http://102.10.10.132/api/Comments/1 
http://102.10.10.132/api/Comments/{newsId}ui

{access_token}url

 
 
 
 
  • 1
  • 2
  • 3

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

须要补全URL,问号后加入access_tokenspa

http://102.10.10.132/api/Comments/1?access_token=1234123 
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}

 
 
 
 
  • 1
  • 2
  • 3
  • 4

@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}

 
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5

@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,将地址以参数的形式传入便可。如
 
 
 
 
  • 1
  • 2
  • 3
  • 4
@GET Call<List<Activity>> getActivityList( @Url String url, @QueryMap Map<String, String> map);
 
 
 
 
  • 1
  • 2

Call<List<Activity>> call = service.getActivityList( "http://115.159.198.162:3001/api/ActivitySubjects", map);

参考简书: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方式实现

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

2.Field Map实现方式

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




相关文章
相关标签/搜索