Retrofit 网络请求学习

来源:互联网 发布:按键精灵安卓淘宝滑动 编辑:程序博客网 时间:2024/06/05 23:51

1 学习使用

Retrofit是Square公司开源的一个高质量高效率的http库,它将我们自己开发的底层的代码和细节都封装了起来。
这里写图片描述

第一步:将Rest API转换为java接口

public interface GitHubService {@GET("users/{user}/repos")Call<List<Repo>> listRepos(@Path("user") String user);// you can add some other meathod}

解释:
这里的Rest API是指服务器端的API,一般会暴露get,post方法
GitHubService其实是对Rest API的一个映射关系,在实际开发中,我们可以定义:public interface ClientService,里面包含post ,get 方法。
接口中的方法使用了Retrofit的注解,Retrofit这个库给了我们很多注解,下一节详细介绍
listRepos这个方法表示:一个get请求获取给定URL的Repo集合。
listRepos传入的参数为我们需要get的url的动态部分。
这里的Repo为我们自己定义的java bean的类:Repo.class用于封装获取的Jason数据。//注意:此处Retrofit又帮我们省掉了很多工作,只需要我们自己定义业务对应的实体类,而Jason数据的转换和封装则帮我们封装好了只需我们调用。

第二步Retrofit会帮我们自动生成接口的实现类的实例,代码如下:

 Retrofit retrofit = new Retrofit.Builder()//01:获取Retrofit对象                .baseUrl("https://api.github.com")//02采用链式结构绑定Base url                .addConverterFactory(GsonConverterFactory.create())                .build();//03执行操作        GitHubService service = retrofit.create(GitHubService.class);//04获取API接口的实现类的实例

解释:

短短的两行代码解决了很多问题
先获取Retrofit对象并绑定基本参数,设置基本配置
一行代码生成API接口的实例对象

第三步:调用接口中定义的业务方法:get post等:

        Call<List<Repo>> call = service.listRepos("lengqi19860101");

解释:
调用接口实现类实例的方法,获取服务器上的数据解析存在List中。

public class GitHubRequestActivity extends AppCompatActivity {    private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) this.findViewById(R.id.stringTv);        Retrofit retrofit = new Retrofit.Builder()//01:获取Retrofit对象                .baseUrl("https://api.github.com")//02采用链式结构绑定Base url                .addConverterFactory(GsonConverterFactory.create())                .build();//03执行操作        GitHubService service = retrofit.create(GitHubService.class);//04获取API接口的实现类的实例        Call<List<Repo>> call = service.listRepos("lengqi19860101");        call.enqueue(new Callback<List<Repo>>() {            @Override            public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {                List<Repo> repoList = response.body();                StringBuffer buffer = new StringBuffer();                for (Repo repo : repoList) {                    buffer.append(repo.full_name + ":" + repo.forks_url);                }                textView.setText(buffer.toString());            }            @Override            public void onFailure(Call<List<Repo>> call, Throwable t) {                textView.setText(t.getMessage());            }        });    }    public interface GitHubService {        @GET("users/{user}/repos")        Call<List<Repo>> listRepos(@Path("user") String user);// you can add some other meathod    }    private class Repo {        String full_name;        String forks_url;    }}

http://download.csdn.net/download/liuhongwei123888/9372394
http://blog.csdn.net/liuhongwei123888/article/details/50375283
http://www.jianshu.com/p/1ef0ba0bccc6

2学习代码原理

https://github.com/hehonghui/android-tech-frontier/tree/master/issue-7/Retrofit%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97
http://www.cnblogs.com/angeldevil/p/3757335.html

0 0