Retrofit之Query注解

来源:互联网 发布:淘宝网div css布局实例 编辑:程序博客网 时间:2024/06/12 23:35

@Query

其实源码的注释已经说的很清楚了,且当做个翻译工吧~
1.Get
一些共同的代码。。先码好

Retrofit retrofit=new Retrofit.Builder()                .baseUrl("http://xxxx")                .addConverterFactory(GsonConverterFactory.create())                .build();Get get=retrofit.create(Get.class);

1)假设你的get请求地址是类似以下格式的:http://xxxx/vvvv/list?page=1
那你可以这样写

public interface Get{    @GET("/vvvv/list")    Call<ResponseBody> list(@Query("page") String page);}Call<ResponseBody> call=get.list("1");

2)假设你的get请求地址是类似以下格式的:http://xxxx/vvvv/list?category=1&category=2

public interface Get{    @GET("/vvvv/list")    Call<ResponseBody> list(@Query("category") String... categories);}Call<ResponseBody> call=get.list("bar", "baz");

3)假设你想传一个这样的参数:”foo+bar”,又不想你的参数中的符号(+号)被URL编码(http://xxxx/vvvv/list?category=foo+bar),可以这样写

public interface Get{    @GET("/vvvv/list")    Call<ResponseBody> list(@Query(value="categories", encoded=true) String categories);}Call<ResponseBody> call=get.list("foo+bar");

另外可能没法用在post上面,暂时还没发现。。。。

1 0
原创粉丝点击