GET請求
多個參數在URL問號以後,且個數不肯定css
http://api.stay4it.com/News?newsId=1&type=類型1…
http://api.stay4it.com/News?newsId={資訊id}&type={類型}…web
@
GET(
"News")
Call<NewsBean> getItem(@QueryMap Map<
String,
String> map);
或者:sql
@
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
@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
@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
@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
@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}
@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}
@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,將地址以參數的形式傳入便可。如
@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);
參考簡書: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方式實現
@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實現方式
@FormUrlEncoded @POST("/posts/create.json") Call<CommitResult> createCommit(@FieldMap Map<String, String> map);