003.Retrofit的使用

2016-12-01  本文已影响15人  春江潮

type-safe HTTP client for Android and Java by Square, Inc

官网地址

引入工程的方式:

compile 'com.squareup.retrofit2:retrofit:2.1.0'

操作方式

将request请求转换成Java接口
public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user); }
这是github暴露的用户仓库接口

Retrofit会帮我们生成一个接口的实现

Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build(); //生成Retrofit 对象 GitHubService service = retrofit.create(GitHubService.class); //在create()方法中生成了接口对象的代理对象,然后调用接口方法

调用自己定义的接口方法

Call<List<Repo>> repos = service.listRepos("octocat");
扩展阅读

上一篇 下一篇

猜你喜欢

热点阅读