retrofit2注解
原创
©著作权归作者所有:来自51CTO博客作者钰娘娘ynn的原创作品,请联系作者获取转载授权,否则将追究法律责任
主要代码来源,参考文献:http://www.jianshu.com/p/bf884248cb37
@GET("xx")
get请求
@Path
替换@GET(“a/{"id"}”)中的大括号
Call<RespnseBody> tt(@Path("id") int id);
@HTTP
万能请求方法
@HTTP(method = "get", path = "blog/{id}", hasBody = false)
method部分可替换为任何种类的请求
@Mutipart上传文件
//传单个文件
@Multipart
@POST("v1/create")
Call<ResponseBody> create(@Part("pictureName") RequestBody pictureName, @Part MultipartBody.Part picture
RequestBody pictureNameBody = RequestBody.create(MediaType.parse(AppConstants.CONTENT_TYPE_FILE), "pictureName");
File picture= new File(path);
RequestBody requestFile = RequestBody.create(MediaType.parse(AppConstants.CONTENT_TYPE_FILE), picture);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part picturePart = MultipartBody.Part.createFormData("picture", picture.getName(), requestFile);
//调接口
create(pictureNameBody, picturePart);
//传多个文件
@Multipart
@POST("v1/create")
Call<ResponseBody> create(@Part("pictureName") RequestBody pictureName, @PartMap Map<String, RequestBody
RequestBody pictureNameBody = RequestBody.create(MediaType.parse(AppConstants.CONTENT_TYPE_FILE), "pictureName");
File picture= new File(path);
RequestBody requestFile = RequestBody.create(MediaType.parse(AppConstants.CONTENT_TYPE_FILE), picture);
Map<String, RequestBody> params = new HashMap<>();
params.put("picture\"; filename=\"" + picture.getName() + "", requestFile);
//调接口
create(pictureNameBody, params);
@Url
完整url
@GET
public void xx(@Url String url);
@FieldMap
@FieldMap Map<String, String> params
Post请求参数,使用@Post时,需要与
连用,
同理,@Field("id") String id
@QueryMap,@Query("query")
用于get