对Retrofit已经使用了一点时间了,是时候归纳一下各种网络请求的service了。
下面分为GET、POST、DELETE还有PUT的请求,说明@Path、@Query、@QueryMap、@Body、@Field的用法。
String BASE_URL = "http://102.10.10.132/api/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.build();
@GET("News")
Call<NewsBean> getItem();
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);
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);
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);
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);
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);
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);
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);
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);
http://102.10.10.132/api/Comments
@HTTP(method = "DELETE",path = "Comments",hasBody = true)
Call<ResponseBody> deleteCommont(
@Body CommentBody body
);
CommentBody
:需要提交的内容,与Post
中的Body
相同
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
@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);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有