ApiService:
public interface ApiService { /** * 结合Retrofit+RxJava * thtp://service.meiyinkeqiu.com/service/ads/cptj * @param * @return */ @GET("ads/cptj") Observable<News> getNoParams(); /** * 结合RxJava * @param user * @return * https://api.github.com/users/forever */ @GET("users/{user}") Observable<User> getHasParams(@Path("user")String user); //第三个接口,,兼容json解析功能,,集成rxjava square retrofit //https://api.github.com/repos/square/retrofit/contributors @GET("repos/{square}/{retrofit}/contributors") Observable<List<Data>> getHasParams2(@Path("square")String square, @Path("retrofit")String retrofit); }Main:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // getNoParams(); getHasParams(); // getHasParams2(); } private void getHasParams2() { Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL).addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//支持RxJava .build(); ApiService apiService = retrofit.create(ApiService.class); //得到Observable Observable<List<Data>> observable = apiService.getHasParams2("square", "retrofit"); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .flatMap(new Func1<List<Data>, Observable<Data>>() { @Override public Observable<Data> call(List<Data> datas) {//一对多的关系 return Observable.from(datas);//循环取Data 代替了迷之缩进的方式获取 } }).subscribe(new Subscriber<Data>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Data data) {//根据被观察生产多少个事件就消费多少个 String avatar_url = data.getAvatar_url(); Log.i("xxx", avatar_url); } }); } private void getHasParams() { Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_URL).addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//支持RxJava .build(); ApiService apiService = retrofit.create(ApiService.class); //得到Observable Observable<User> observable = apiService.getHasParams("forever"); observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .map(new Func1<User, User>() {//直接变换 一对一的关系 @Override public User call(User user) { return user; } }).subscribe(new Subscriber<User>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(User user) { String avatar_url = user.getAvatar_url(); Log.i("xxx", avatar_url); } }); } private void getNoParams() { Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.BASE_PATH).addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//支持RxJava .build(); ApiService apiService = retrofit.create(ApiService.class); //得到Observable Observable<News> observable = apiService.getNoParams();//生产事件 //被观察者订阅观察 默认在同一个线程 observable.subscribeOn(Schedulers.io())//指定IO做耗时操作 .observeOn(AndroidSchedulers.mainThread())//指定更新UI在主线程 .subscribe(new Observer<News>() { @Override public void onCompleted() {//完成 } @Override public void onError(Throwable e) {//失败 Log.i("xxx", e.getMessage()); } @Override public void onNext(News news) {//消费事件 List<News.AdsBean> ads = news.getAds(); for (int i = 0; i < ads.size(); i++) { News.AdsBean adsBean = ads.get(i); String gonggaoren = adsBean.getGonggaoren(); Log.i("xxx", gonggaoren); } } }); } }Api:
public class Api { public static final String BASE_PATH = "http://service.meiyinkeqiu.com/service/"; public static final String BASE_URL = "https://api.github.com/"; }compile 'com.squareup.retrofit2:retrofit:2.0.1' compile 'com.squareup.retrofit2:converter-gson:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.okhttp3:logging-interceptor:3.4.1' compile 'com.squareup.retrofit2:retrofit:2.0.1'