- @Path:所有在網(wǎng)址中的參數(shù)(URL的問號(hào)前面)請(qǐng)求的相對(duì)地址
請(qǐng)求的相對(duì)地址也是需要調(diào)用方傳遞,通過Path注解可以在具體的
調(diào)用場(chǎng)景中動(dòng)態(tài)傳遞 - @Query:URL問號(hào)后面的參數(shù)
- @QueryMap: 相當(dāng)于多個(gè)@Query
- @Field:Post請(qǐng)求需要把請(qǐng)求參數(shù)放置在請(qǐng)求體中,而非拼接在url
后面 (使用@Field時(shí)記得添加@FormUrlEncoded) - @Body:相當(dāng)于多個(gè)@Field,以對(duì)象的形式提交
兩種requestBody,一個(gè)是FormBody,一個(gè)是MultipartBody,前者以表單的方式傳遞簡(jiǎn)單的鍵值對(duì),后者以POST表單的方式上傳文件可以攜帶參數(shù)。
-
@FormUrlEncoded:表單的方式傳遞鍵值對(duì)
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:?jiǎn)挝募蟼?br> 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的意思就是允許多個(gè)@Part了,我們這里使用了3個(gè)@Part,第一個(gè)我們準(zhǔn)備上傳個(gè)文件,使用了MultipartBody.Part類型,其余兩個(gè)均為簡(jiǎn)單的鍵值對(duì)。
-
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請(qǐng)求
樣式1、一個(gè)簡(jiǎn)單的get請(qǐng)求
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);
或者多個(gè)參數(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問號(hào)之后
http://102.10.10.132/api/News?newsId={資訊id}
@GET("News")
Call<NewsBean> getItem(@Query("newsId") String newsId);
或者多個(gè):http://102.10.10.132/api/News?newsId={資訊id}&type={類型}
@GET("News")
Call<NewsBean> getItem(@Query("newsId") String newsId, @Query("type") String type);
-樣式4、多個(gè)參數(shù)在URL問號(hào)之后,且個(gè)數(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請(qǐng)求
樣式1、需要補(bǔ)全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、需要補(bǔ)全URL,問號(hào)后加入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、需要補(bǔ)全URL,問號(hào)后加入access_token,post一個(gè)body(對(duì)象)
@POST("Comments/{newsId}")
Call<Comment> reportComment(
@Path("newsId") String commentId,
@Query("access_token") String access_token,
@Body CommentBean bean);