retrofit 網(wǎng)絡(luò)請求的service

  • @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請求

-樣式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);

參考:http://m.itdecent.cn/p/7687365aa946

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容