Retrofit和Rxjava的简单使用

来源:互联网 发布:火车头采集js分页 编辑:程序博客网 时间:2024/05/16 01:07

Retrofit与RxJava的简单使用

    retrofit是基与okhttp框架进行二次封装后的一个网络请求工具,如果单独作为网络请求来使用,我认为跟xutils、volley、okhttp并无太大的区别,只是volley在请求后的返回参数的方法里,这个方法中可以直接进行UI更新,并不需要handler通知主线程,因为它所在的线程就是主线程,所以对于xutils和okhttp来说,这是它的一个方便之处。而对于retrofit来讲,它的一个优点在于它对请求的封装,对于我们开发来讲,一个项目中大大小小也有几十次http请求,所以有一个良好的封装请求的类对于代码的简洁有了很大的帮助,使用retrofit时要新建一个请求接口,在这个接口里面去填写需要访问的某个地址的参数,话不多说,具体如下,拿豆瓣电影的数据接口为例,首先是retrofit和RxJava所需要的依赖retrofit为2.0.0,RxJava为1.0.0,单独retrofit不需要RxJava的依赖.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'io.reactivex:rxjava:1.1.0'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
   compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
   compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.jakewharton:butterknife:7.0.1'
}

然后就是创建接口数据的实体类,通过豆瓣电影的数据接口:获取实体类
        https://api.douban.com/v2/movie/top250?start=0&count=10
public class MovieDao {    /**     * count : 10     * start : 0     * total : 250     * subjects :     * title : 豆瓣电影Top250     */    private int count;    private int start;    private int total;    private String title;    private List<SubjectsBean> subjects;    public int getCount() {        return count;    }

由于数据太多,这里只贴部分,接下来是定义http请求接口:
public interface APIService {/** * https://api.douban.com/v2/movie/top250?start=0&count=10 */    @GET("v2/movie/top250")    Call<MovieDao> loadMovie(@Query("start") int start,@Query("count") int count);    @GET("v2/movie/top250")    Observable<MovieDao> getMovieData(@Query("start") int start, @Query("count") int count);}

其中@get中的括号里表示请求地址中的域名的后部分,域名在retrofit中写在baseurl中,定义的方法中的@query后面表示填入的参数及参数类型,请求接口写好后,最后在代码中配置请求信息:

@Overridepublic void onClick(View v) {    Retrofit retrofit = new Retrofit.Builder()            .baseUrl(baseUrl)            .addConverterFactory(GsonConverterFactory.create())            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())            .build();    APIService apiService = retrofit.create(APIService.class);    apiService.getMovieData(0,10)            .subscribeOn(Schedulers.io())            .observeOn(AndroidSchedulers.mainThread())            .subscribe(new Subscriber<MovieDao>() {                @Override                public void onCompleted() {                    Toast.makeText(getActivity(),"完成",Toast.LENGTH_SHORT).show();                }                @Override                public void onError(Throwable e) {                }                @Override                public void onNext(MovieDao movieDao) {                    Toast.makeText(getActivity(), movieDao.getTitle(),Toast.LENGTH_SHORT).show();                }            });}

其中addConverterFactory添加对Gson解析的支持,addCallAdapterFactory是添加对RxJava的支持,subscribeOn是将任务执行在io线程中,observeOn即使回到主线程,subscribe回调方法中的OnCompleted即是请求完成,可更新ui
0 0
原创粉丝点击