Android】Retrofit網絡請求參數注解,@Path、@Query、@QueryMap...(轉)


對Retrofit已經使用了一點時間了,是時候歸納一下各種網絡請求的service了。

下面分為GET、POST、DELETE還有PUT的請求,說明@Path、@Query、@QueryMap、@Body、@Field的用法。

初始化Retrofit
String BASE_URL = "http://102.10.10.132/api/"; Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .build(); 

GET

樣式1(一個簡單的get請求)

http://102.10.10.132/api/News

    @GET("News") Call<NewsBean> getItem(); 

樣式2(URL中有參數)

http://102.10.10.132/api/News/1
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/1/類型1
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=1
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=1&type=類型1
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=1&type=類型1...
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/1
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/1?access_token=1234123
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(對象))

http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{newsId}?access_token={access_token}

    @POST("Comments/{newsId}") Call<Comment> reportComment( @Path("newsId") String commentId, @Query("access_token") String access_token, @Body CommentBean bean); 

DELETE

樣式1(需要補全URL)

http://102.10.10.132/api/Comments/1
http://102.10.10.132/api/Comments/{commentId}

    @DELETE("Comments/{commentId}") Call<ResponseBody> deleteNewsCommentFromAccount( @Path("commentId") String commentId); 

樣式2(需要補全URL,問號后加入access_token)

http://102.10.10.132/api/Comments/1?access_token=1234123
http://102.10.10.132/api/Comments/{commentId}?access_token={access_token}

    @DELETE("Comments/{commentId}") Call<ResponseBody> deleteNewsCommentFromAccount( @Path("commentId") String commentId, @Query("access_token") String access_token); 

樣式3(帶有body)

http://102.10.10.132/api/Comments

@HTTP(method = "DELETE",path = "Comments",hasBody = true) Call<ResponseBody> deleteCommont( @Body CommentBody body ); 

CommentBody:需要提交的內容,與Post中的Body相同

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

  • Tips1
    使用@Field時記得添加@FormUrlEncoded
  • Tips2
    若需要重新定義接口地址,可以使用@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); 

希望對大家有幫助,打賞什么的就不用了



作者:帶心情去旅行
鏈接:https://www.jianshu.com/p/7687365aa946
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM