public interface GitHubService {
@GET("users/{user}/repos")
Call<List<Repo>> listRepos(@Path("user") String user);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/") //baseUrl須以“/”結(jié)尾?
.build();
//內(nèi)部生成動態(tài)代理類
GitHubService service = retrofit.create(GitHubService.class);
Call<List<Repo>> repos = service.listRepos("octocat");
元芳,你怎么看?
GET注解:說明這是一個GET請求
GET傳入?yún)?shù):是一個路徑,注意,里面有一個占位符。
listRepos()方法傳入的參數(shù)用來進(jìn)行配置的,具體用來配置什么,由各自的注解內(nèi)容決定。例子中, @Path(“user”)說明user參數(shù)用來配置(替換)路徑里的user占位符的
假定接口另一個方法
@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);
options自然也是用來“配置”的,在動態(tài)代理類的生成過程中會用到這個參數(shù),在哪個點(diǎn)會用到這個參數(shù)呢?自然就是根據(jù)它的注解的指定的。
假如還有其他的參數(shù),基本類似。