- @Path:所有在網(wǎng)址中的參數(shù)(URL的問號前面)請求的相對地址
請求的相對地址也是需要調(diào)用方傳遞,通過Path注解可以在具體的
調(diào)用場景中動態(tài)傳遞 - @Query:URL問號后面的參數(shù)
- @QueryMap: 相當(dāng)于多個@Query
- @Field:Post請求需要把請求參數(shù)放置在請求體中,而非拼接在url
后面 (使用@Field時記得添加@FormUrlEncoded) - @Body:相當(dāng)于多個@Field,以對象的形式提交
兩種requestBody,一個是FormBody,一個是MultipartBody,前者以表單的方式傳遞簡單的鍵值對,后者以POST表單的方式上傳文件可以攜帶參數(shù)。
-
@FormUrlEncoded:表單的方式傳遞鍵值對
public interface IUserBiz
{
@POST("login")
@FormUrlEncoded
Call<User> login(@Field("username") String username, @Field("password") String password);
}/省略retrofit的構(gòu)建代碼 Call<User> call = userBiz.login("zhy", "123"); //省略call執(zhí)行相關(guān)代碼 -
@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,第一個我們準(zhǔn)備上傳個文件,使用了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中有參數(shù)
http://102.10.10.132/api/News/{資訊id}
@GET("News/{newsId}")
Call<NewsBean> getItem(@Path("newsId") String newsId);
或者多個參數(shù):http://102.10.10.132/api/News/{資訊id}/{類型}
@GET("News/{newsId}/{type}")
Call<NewsBean> getItem(@Path("newsId") String newsId, @Path("type") String type);樣式3、參數(shù)在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、多個參數(shù)在URL問號之后,且個數(shù)不確定
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的數(shù)據(jù)只有一條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的數(shù)據(jù)只有一條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);