[web] WEB开发笔记

WEB学习笔记

HTTP 操作说明

GET:显示服务器资源
POST:创建相关资源
DELETE: 删除相关资源
PUT/PATCH:更新相关资源

Retrofit网络请求参数用法说明(转载)

原文地址

初始化Retrofit

Retrofit retrofit =newRetrofit.Builder()
.baseUrl("https://fanyi-api.baidu.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
  1. GET
    样式1(一个简单的get请求)
http://192.168.1.1/api/News
@GET("News")
Call  getItem();

样式2(URL中有参数)

http://192.168.1.1/api/News/1
http://192.168.1.1/api/News/{资讯id}
@GET("News/{newsId}")
Call getItem(@Path("newsId")String newsId);
或
http://192.168.1.1/api/News/1/类型1
http://192.168.1.1/api/News/{资讯id}/{类型}
@GET("News/{newsId}/{type}")
Call getItem(@Path("newsId")String newsId, @Path("type")String type);

样式3(参数在URL问号之后)

http://192.168.1.1/api/News?newsId=1
http://192.168.1.1/api/News?newsId={资讯id}
@GET("News")
Call getItem(@Query("newsId") String newsId);
或
http://192.168.1.1/api/News?newsId=1&type=类型1
http://192.168.1.1/api/News?newsId={资讯id}&type={类型}
@GET("News")
Call getItem(@Query("newsId") String newsId, @Query("type") String type);

样式4(多个参数在URL问号之后,且个数不确定)

http://192.168.1.1/api/News?newsId=1&type=类型1...
http://192.168.1.1/api/News?newsId={资讯id}&type={类型}...
@GET("News")
Call getItem(@QueryMap Map map);
也可以
@GET("News")
Call getItem(@Query("newsId")String newsId,@QueryMap Map map);
  1. POST
    样式1(需要补全URL,post的数据只有一条reason)
http://192.168.1.1/api/Comments/1
http://192.168.1.1/api/Comments/{newsId}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call reportComment(@Path("newsId") String commentId,@Field("reason") String reason);

样式2(需要补全URL,问号后加入access_token,post的数据只有一条reason)

http://192.168.1.1/api/Comments/1?access_token=1234123
http://192.168.1.1/api/Comments/{newsId}?access_token={access_token}
@FormUrlEncoded
@POST("Comments/{newsId}")
Call reportComment(@Path("newsId") String commentId,@Query("access_token") String access_token,@Field("reason") String reason);

样式3(需要补全URL,问号后加入access_token,post一个body(对象))

http://192.168.1.1/api/Comments/1?access_token=1234123
http://192.168.1.1/api/Comments/{newsId}?access_token={access_token}
@POST("Comments/{newsId}")
Call reportComment(@Path("newsId") String commentId, @Query("access_token") String access_token,@Body CommentBean bean);
  1. DELETE
    样式1(需要补全URL)
http://192.168.1.1/api/Comments/1
http://192.168.1.1/api/Comments/{newsId}
{access_token}
@DELETE("Comments/{commentId}")
Call deleteNewsCommentFromAccount(@Path("commentId") String commentId);

样式2(需要补全URL,问号后加入access_token)

http://192.168.1.1/api/Comments/1?access_token=1234123
http://192.168.1.1/api/Comments/{newsId}?access_token={access_token}
@DELETE("Comments/{commentId}")
Call deleteNewsCommentFromAccount(@Path("accountId")String accountId,        @Query("access_token")String access_token);
  1. PUT(这个请求很少用到,例子就写一个)
http://192.168.1.1/api/Accounts/1
http://192.168.1.1/api/Accounts/{accountId}
@PUT("Accounts/{accountId}")
Call updateExtras(@Path("accountId")String accountId,        @Query("access_token")String access_token, @Body ExtrasBean bean);

总结

@Path:所有在网址中的参数(URL的问号前面),如:
http://192.168.1.1/api/Accounts/{accountId}
@Query:URL问号后面的参数,如:
http://192.168.1.1/api/Comments?access_token={access_token}
@QueryMap:相当于多个@Query
@Field:用于POST请求,提交单个数据
@FieldMap:以map形式提交多个Field(Retrofit2.0之后添加)
@Body:相当于多个@Field,以对象的形式提交
TIps
Tip1
使用@Field时记得添加@FormUrlEncoded
Tip2
若需要重新定义接口地址,可以使用@Url,将地址以参数的形式传入即可。
Tip3
@Path 和@Query的区别
相同点:都是请求头中的带有的数据
不同点:前者是请求头中问号之前用于替换URL中变量的字段,后者是请求头问号之后用于查询数据的字段,作用和应用场景都不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值