android 注解表单,Android - Retrofit注解

本文将Retrofit注解完全解析一下,好记性不如烂笔头。

注解分类:

Retrofit注解分为三类。分别是Http请求方法注解、标记类注解和参数类注解。

Http请求方式注解8种:GET 、POST、 PUT、 DELETE、 HEAD、 PATCH、 OPTIONS 和HTTP。前7中分别代表Http的请求方法,Http则可以替换以上7中,也可以拓展请求方法。

2.标记类注解3种:FormUrlEncode、Multipart和 Streaming。

参数类注解10种:Header、Headers、Body、Path、Field、FieldMap、Part、PartMap、Query、QueryMap和URL。

Retrofit初始化:

String url = "http://102.10.10.132/api/";

Retrofit retrofit = new Retrofit.Builder()

.baseUrl(url)

.addConverterFactory(GsonConverterFactory.create())

.build();

GET请求:

public interface RetrofitService {

@GET("news")

Call getDate();

}

public interface RetrofitService {

@GET("news")

Call getDate(@Path("name") String name);

}

public interface RetrofitService {

@GET("news")

Call getDate(@Path("name") String name, @Path("type")String type);

}

一个或多个参数在问号之后

public interface RetrofitService {

@GET("news")

Call getDate(@Query("name") String nameStr);

}

public interface RetrofitService {

@GET("news")

Call getDate(@Query("name") String nameStr, @Query("type")String typeStr);

}

public interface RetrofitService {

@GET("news")

Call getDate(@QueryMap Map params);

}

POST请求:

public interface RetrofitService {

@FormUrlEncoded

@POST("Comment/{id}")

Call getDate(@Path("id") String CommentId, @Field("reason") String reason);

}

public interface RetrofitService {

@FormUrlEncoded

@POST("Comment/{id}")

Call getDate(@Path("id") String CommentId,@Query("token") String token, @Field("reason") String reason);

}

public interface RetrofitService {

@FormUrlEncoded

@POST("Comment/{id}")

Call getDate(@Path("id") String CommentId,@Query("token") String token, @Body T t);

}

public interface RetrofitService {

@Multipart

@POST("Comment")

Call getDate(@Part MultipartBody.Part photo,@Part("description")RequestBody description);

}

DELETE请求:

public interface RetrofitService {

@DELETE("Delete/{DeleteId}")

Call getDate(@Path("DeleteId") String CommentId);

}

public interface RetrofitService {

@DELETE("Delete/{DeleteId}")

Call getDate(@Path("DeleteId") String CommentId,@Query("token") String token);

}

public interface RetrofitService {

@HTTP(method = "DELETE",path = "Comments",hasBody = true)

Call getDate(@Body T t);

}

用自己的理解总结:

Path相当于参数替换

Field一般用于POST请求中,后面跟要提交的表单,并且添加@ FormUrlEncoded搭配使用。

Body 一般用于POST请求中,相当于多个Field,以对象的方式提交。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值