Retrofit中的注解
@Query,@QueryMap,@Field,@FieldMap,@FormUrlEncoded,@Path,@Url
这七种注解应该是最常用的了。
下边列举各种应用场景。

Retrofit retrofit =newRetrofit.Builder()
.baseUrl("https://api.github.com/")
.build();publicinterfaceGitHubService {//无参数@GET("users/stven0king/repos")
Call>listRepos();//少数参数@GET("users/stven0king/repos")
Call> listRepos(@Query("time")longtime);//参数较多@GET("users/stven0king/repos")
Call> listRepos(@QueryMap Mapparams);
}

@Query和@QueryMap也可以结合在一起使用。
要是对应的url在服务端支持get/post两种类型的请求的话,那么上面的@GET变为@POST也可以执行,只不过post请求时所带的参数也会像get方式一样已?key=value&key1=vaule2…的形式拼接在url的后边。
二、post方式请求静态url地址

Retrofit retrofit =newRetrofit.Builder()
.baseUrl("https://api.github.com/")
.build()publicinterfaceGitHubService {//无参数@POST("users/stven0king/repos")
Call>listRepos();//少数参数@FormUrlEncoded
@POST("users/stven0king/repos")
Call> listRepos(@Field("time")longtime);//参数较多@FormUrlEncoded
@POST("users/stven0king/repos")
Call> listRepos(@FieldMap Mapparams);
}

@Field和@FieldMap可以结合在一起使用。
另外是不是发现了比起@GET多了一个@FromUrlEncoded的注解。如果去掉@FromUrlEncoded在post请求中使用@Field和@FieldMap,那么程序会抛出java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1)的错误异常。如果将@FromUrlEncoded添加在@GET上面呢,同样的也会抛出java.lang.IllegalArgumentException:FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST).的错误异常
三、半静态的url地址请求

Retrofit retrofit =newRetrofit.Builder()
.baseUrl("https://api.github.com/")
.build()publicinterfaceGitHubService {
@GET("users/{user}/repos")
Call> listRepos(@Path("user") String user);
}

四、动态的url地址请求

Retrofit retrofit =newRetrofit.Builder()
.baseUrl("https://api.github.com/")
.build()publicinterfaceGitHubService {
@GET
Call>listRepos(@Url String user);
}

五、总结小细节
当@GET或@POST注解的url为全路径时(可能和baseUrl不是一个域),会直接使用注解的url的域。
如果请求为post实现,那么最好传递参数时使用@Field、@FieldMap和@FormUrlEncoded。因为@Query和或QueryMap都是将参数拼接在url后面的,而@Field或@FieldMap传递的参数时放在请求体的。
使用@Path时,path对应的路径不能包含”/”,否则会将其转化为%2F。在遇到想动态的拼接多节url时,还是使用@Url吧。