Android框架学习之Retrofit(二)RxJava和Retrofit2.0的结合使用

来源:互联网 发布:网络歌手排行榜2005 编辑:程序博客网 时间:2024/05/25 18:10
  1. 添加依赖
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'compile 'io.reactivex:rxjava:1.1.0'compile 'io.reactivex:rxandroid:1.1.0'
  1. 接口
@GET("showapi_open_bus/showapi_joke/joke_text")    Observable<ArrayList<JokeBean>> listJokesRx(@Query("page") String page);
  1. 使用
  final  String BASEURL = "http://apis.baidu.com/";        new Retrofit.Builder()                .baseUrl(BASEURL)                .addConverterFactory(MyConverterFactory.create())                .build()                .create(JokeService.class)                .listJokesRx(1+"")                .subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Subscriber<ArrayList<JokeBean>>() {                    @Override                    public void onCompleted() {                    }                    @Override                    public void onError(Throwable e) {                    }                    @Override                    public void onNext(ArrayList<JokeBean> jokeBeen) {                    }                });        //        Retrofit retrofit = new Retrofit.Builder()                .baseUrl("http://apis.baidu.com/")                .addConverterFactory(MyConverterFactory.create())                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())                .build();        //        JokeService service = retrofit.create(JokeService.class);        //        Observable<ArrayList<JokeBean>> observable = service.listJokesRx("" + 1);        //        observable.subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<ArrayList<JokeBean>>() {            @Override            public void onCompleted() {                Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_SHORT).show();            }            @Override            public void onError(Throwable e) {                e.printStackTrace();                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();            }            @Override            public void onNext(ArrayList<JokeBean> jokeBeans) {                Log.e("Retrofit", "requestRxJava:" + jokeBeans.toString());            }        });
0 0
原创粉丝点击