RxJava+Retrofit的瀑布流Demo

前言 学习RxJava+Retrofit的开发框架之余,做了一个小的Demo。主要功能是获取图片数据,以瀑布流形式展示。项目效果:

项目效果.gif 代码实现: 网络请求的对外接口通过HttpManager这个类访问:

public class HttpManager {//base http service private HttpService httpService; //构造方法私有 private HttpManager() { httpService = RetrofitClient.getInstance().getHttpService(HttpService.class); }/** * create singleton instance */ private static class SingletonHolder { private static final HttpManager INSTANCE = new HttpManager(); }/** * obtain the instance * * @return */ public static HttpManager getInstance() { return SingletonHolder.INSTANCE; }public void getImages(String query,int startIndex,int endIndex,Subscriber subscriber){ Observable observable = httpService.getImageData(query,startIndex,endIndex); RetrofitClient.getInstance().toSubscribe(observable,subscriber); } }

其中httpService是Retrofit的接口类:
public interface HttpService {@GET("j") Observable getImageData(@Query("q") String q,@Query("sn") int sn,@Query("pn") int pn); }

Retrofit以接口的形式定义需要的网络请求。
在HttpManager的构造方法中:
//构造方法私有 private HttpManager() { httpService = RetrofitClient.getInstance().getHttpService(HttpService.class); }

【RxJava+Retrofit的瀑布流Demo】这里我们将自定义的HttpService接口类,与Retrofit进行关联,目的是告诉Retrofit用什么请求方法,什么参数,返回结果长什么样。
RetrofitClient中对Retrofit的一些基本参数做配置:
public class RetrofitClient {private Retrofit retrofit; private RetrofitClient() { }/** * create singleton instance */ private static class SingletonHolder { private static final RetrofitClient INSTANCE = new RetrofitClient(); }/** * obtain the instance * * @return */ public static RetrofitClient getInstance() { return SingletonHolder.INSTANCE; }/** * get base http Service * * @param clazz * @param class which need to init * @return return formatted instance of Class of T */ public T getHttpService(Class clazz) { return getHttpService(clazz, HttpConsts.BASE_URL); }public T getHttpService(Class clazz, String baseUrl) { return getHttpService(clazz, baseUrl, GsonConverterFactory.create()); }public T getHttpService(Class clazz, String baseUrl, Converter.Factory factory) { OkHttpClient.Builder okBuilder = new OkHttpClient.Builder() .connectTimeout(HttpConsts.TIMEOUT, TimeUnit.MILLISECONDS); Retrofit.Builder builder = new Retrofit.Builder(); builder.client(okBuilder.build()); builder.addCallAdapterFactory(RxJavaCallAdapterFactory.create()); builder.baseUrl(baseUrl); if (factory != null) { builder.addConverterFactory(factory); } retrofit = builder.build(); return retrofit.create(clazz); }/** * config to base subscribe * * @param observable * @param subscriber * @param */ public void toSubscribe(Observable observable, Subscriber subscriber) { observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber); }}

这里有两个方法。一个是getHttpService方法,在这个方法中加入了GsonConverterFactory进行json转换,加入了RxJavaCallAdapterFactory对RxJava做支持。另一个方法toSubscribe方法是为网络请求的结果处理提供一个回调。
实际的网络请求如下:
public void getImages(String query,int startIndex,int endIndex,Subscriber subscriber){ Observable observable = httpService.getImageData(query,startIndex,endIndex); RetrofitClient.getInstance().toSubscribe(observable,subscriber); }

这里的observable已经是Retrofit网络请求回来的结果。后面通过toSubscribe方法将结果的处理回调给外部调用者。
工程地址:https://github.com/PaulZJ/WaterFallFlow

    推荐阅读