一身转战三千里,一剑曾百万师。这篇文章主要讲述Android--Retrofit+RxJava相关的知识,希望能为你提供帮助。
1,现在响应式编程也是越来越多在项目中使用了,刚好上篇我们简单了介绍了一下Retrofit,那么我们来开始试着两个一起用吧,不过不了解Rxjava的同学可以先去看一看这个文章(挺好的):http://gank.io/post/560e15be2dca930e00da1083#toc_27
2,接着我们上一个项目的写,怎么添加RxJava到我们网络请求框架中呢,首先我们得修改Service的代码为
public interface MovieService { @GET("top250") Observable< MovieEntity> getTopMovie(@Query("start") int start, @Query("count") int count); }
然后我们Activity中的getMovie()方法修改为
package com.qianmo.retrofitdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.qianmo.retrofitdemo.entry.MovieEntity; import com.qianmo.retrofitdemo.http.MovieService; import butterknife.Bind; import butterknife.ButterKnife; import butterknife.OnClick; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; public class MainActivity extends AppCompatActivity {@Bind(R.id.tv_show) TextView tvShow; @Bind(R.id.btn_request) Button btnRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); }@OnClick({R.id.btn_request}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_request: getMovie(); break; } }//请求网络数据 private void getMovie() {////https://api.douban.com/v2/movie/top250?start=0& count=10 目标地址 //String baseUrl = "https://api.douban.com/v2/movie/"; // ////创建Retrofit对象 //Retrofit retrofit = new Retrofit.Builder() //.baseUrl(baseUrl) //.addConverterFactory(GsonConverterFactory.create()) //.build(); // //MovieService movieService = retrofit.create(MovieService.class); //Call< MovieEntity> call = movieService.getTopMovie(0, 10); //call.enqueue(new Callback< MovieEntity> () { //@Override //public void onResponse(Call< MovieEntity> call, Response< MovieEntity> response) { //tvShow.setText(response.body().getTitle()); //} // //@Override //public void onFailure(Call< MovieEntity> call, Throwable t) { //tvShow.setText(t.getMessage()); //} //}); String baseUrl = "https://api.douban.com/v2/movie/"; Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); MovieService movieService = retrofit.create(MovieService.class); movieService.getTopMovie(0, 10) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber< MovieEntity> () { @Override public void onCompleted() { Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show(); }@Override public void onError(Throwable e) { tvShow.setText(e.getMessage()); }@Override public void onNext(MovieEntity movieEntity) { tvShow.setText(movieEntity.getTitle()); } }); }}
【Android--Retrofit+RxJava】这样我们就简单的把RxJava集成过来了
推荐阅读
- android快速开发--常用utils类
- Android常用库
- Cocos2d-x 3.x部署到安卓
- Android 系统版本&API对照表
- Android严苛模式StrictMode使用详解
- Android测试流量的几种方法
- Android实用视图动画及工具系列之九(漂亮的图片选择器,高性能防崩溃图片选择工具)
- android 监听网络状态的变化及实战
- Android Listview 性能优化