- @Path:所有在網址中的參數(URL的問號前面)請求的相對地址
請求的相對地址也是需要調用方傳遞,通過Path注解可以在具體的
調用場景中動態(tài)傳遞 - @Query:URL問號后面的參數
- @QueryMap: 相當于多個@Query
- @Field:Post請求需要把請求參數放置在請求體中赂鲤,而非拼接在url
后面 (使用@Field時記得添加@FormUrlEncoded) - @Body:相當于多個@Field,以對象的形式提交
兩種requestBody泉褐,一個是FormBody,一個是MultipartBody鸟蜡,前者以表單的方式傳遞簡單的鍵值對膜赃,后者以POST表單的方式上傳文件可以攜帶參數。
-
@FormUrlEncoded:表單的方式傳遞鍵值對
public interface IUserBiz
{
@POST("login")
@FormUrlEncoded
Call<User> login(@Field("username") String username, @Field("password") String password);
}/省略retrofit的構建代碼 Call<User> call = userBiz.login("zhy", "123"); //省略call執(zhí)行相關代碼
-
@MultipartBody:單文件上傳
public interface IUserBiz
{
@Multipart
@POST("register")
Call<User> registerUser(@Part MultipartBody.Part photo, @Part("username") RequestBody username, @Part("password") RequestBody password);
}File file = new File(Environment.getExternalStorageDirectory(), "icon.png"); RequestBody photoRequestBody =RequestBody.create(MediaType.parse("image/png"), file); MultipartBody.Part photo = MultipartBody.Part.createFormData("photos", "icon.png", photoRequestBody); Call<User> call = userBiz.registerUser(photo, RequestBody.create(null, "abc"), RequestBody.create(null, "123"));
這里@MultiPart的意思就是允許多個@Part了揉忘,我們這里使用了3個@Part跳座,第一個我們準備上傳個文件,使用了MultipartBody.Part類型泣矛,其余兩個均為簡單的鍵值對疲眷。
-
PartMap 多文件上傳
public interface IUserBiz
{
@Multipart
@POST("register")
Call<User> registerUser(@PartMap Map<String, RequestBody> params, @Part("password") RequestBody password);
}File file = new File(Environment.getExternalStorageDirectory(), "messenger_01.png"); RequestBody photo = RequestBody.create(MediaType.parse("image/png", file); Map<String,RequestBody> photos = new HashMap<>(); photos.put("photos\"; filename=\"icon.png", photo); photos.put("username", RequestBody.create(null, "abc")); Call<User> call = userBiz.registerUser(photos, RequestBody.create(null, "123"));
GET請求
樣式1、一個簡單的get請求
http://102.10.10.132/api/News
@GET("News")
Call<NewsBean> getItem();樣式2您朽、URL中有參數
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/{資訊id}/{類型}
@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={資訊id}
@GET("News")
Call<NewsBean> getItem(@Query("newsId") String newsId);
或者多個:http://102.10.10.132/api/News?newsId={資訊id}&type={類型}
@GET("News")
Call<NewsBean> getItem(@Query("newsId") String newsId哗总, @Query("type") String type);
-樣式4几颜、多個參數在URL問號之后,且個數不確定
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);
POST請求
樣式1蛋哭、需要補全URL,post的數據只有一條reason
http://102.10.10.132/api/Comments/{newsId}
@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/{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);樣式3叛本、需要補全URL沪蓬,問號后加入access_token,post一個body(對象)
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Body CommentBean bean);