使用RxJava+Retrofit+MVP+Glide开发一个简单的新闻客户端
2016-11-18 12:37
686 查看
好久没有更新博客了,有点懒。。之前在网上看到很多有趣又高效的开源库,于是就想着写一个简单的项目来运用这些开源库,恰巧看见聚合数据上有个新闻头条的API,所以就尝试写了一下。先上效果图:
在这个小项目中,使用RxJava和Retrofit网络请求框架来实现异步数据的获取是比较重要的环节。
关于RxJava和Retrofit的使用可以参考:http://gank.io/post/56e80c2c677659311bed9841
首先,需要封装好网络请求逻辑:
网络请求封装好了,由于整体采用了MVP架构,所以在Model层需要异步请求数据:
整个项目设计中也同时实现了简单的事件总线RxBus,用于发送用户定义的Event:
更多细节也可以参考:http://git.oschina.net/QiHuangQi/News
在这个小项目中,使用RxJava和Retrofit网络请求框架来实现异步数据的获取是比较重要的环节。
关于RxJava和Retrofit的使用可以参考:http://gank.io/post/56e80c2c677659311bed9841
首先,需要封装好网络请求逻辑:
public class Api { private Retrofit retrofit; private static ApiService apiService; private Api() { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .build(); retrofit = new Retrofit.Builder() .baseUrl(AppConfig.baseUrl) .addConverterFactory(GsonConverterFactory.create()) //添加Rx适配 .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build(); apiService = retrofit.create(ApiService.class); } public static ApiService getDefault() { if (apiService == null) { synchronized (Api.class) { if (apiService == null) { new Api(); } } } return Api.apiService; } }
public interface ApiService { //获取新闻请求,返回Observable对象 @GET("index") Observable<News> getNews(@Query("type") String type, @Query("key") String apiKey); }
网络请求封装好了,由于整体采用了MVP架构,所以在Model层需要异步请求数据:
public class NewsModel implements NewsContract.Model { @Override public Observable<List<Data>> getChannelList(String type) { return Api.getDefault() .getNews(type, AppConfig.apiKey) .map(news -> news.getResult().getData()) .compose(RxSchedulers.io_main()); //对Observable进行转换,用于切换执行异步任务的线程 } }
整个项目设计中也同时实现了简单的事件总线RxBus,用于发送用户定义的Event:
public class RxBus { private static RxBus rxBus; private RxBus() { } public static RxBus getInstance() { if (rxBus == null) { synchronized (RxBus.class) { if (rxBus == null) { rxBus = new RxBus(); } } } return rxBus; } public final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create()); public final Vector<Subscription> subscriptions = new Vector<>(); //发送事件 public void send(Object o) { _bus.onNext(o); } //取出事件 public Observable<Object> toObserverable() { return _bus; } //添加订阅 public void addSubscription(Subscription s) { subscriptions.add(s); } //取消所有的订阅 public void unSubscribeAll() { for (Subscription s : subscriptions) { if (!s.isUnsubscribed()) { s.unsubscribe(); } } } }
更多细节也可以参考:http://git.oschina.net/QiHuangQi/News
相关文章推荐
- 关于ListView控件使用的个人总结--以一个简单的新闻客户端开发为例
- 如何优雅的使用Retrofit、Rxjava、Butterknife、Material开发一款MVP模式的新闻+天气预报+妹子的Android app
- Android开发——使用BaseAdapter做一个简单的新闻效果
- 一款基于Retrofit + RxJava + MVC架构开发的新闻客户端
- Android Material Design + MVP + Rxjava + Retrofit + Okhttp + Glide一个小项目
- Kotlin Anko Layout+MVP(Glide,Retrofit,OkHttp,RxJava)开发Android运用程序
- 浅学开发模式----MVP模式和使用Retrofit和RxJava使用
- Android 框架设计Demo,一个简单的MVP示例搜索功能,网络请求用Retrofit+RxJava实现
- 使用Retrofit+RxJava搭建简单的MVP网络请求框架
- Android-MVP+Retrofit+Rxjava实现一个知乎日报客户端
- MVP+retrofit+rxjava+Knife的简单使用
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- 使用TCP+winsock写的一个简单客户端程序
- 使用jrtplib写的一个简单服务器和客户端程序
- [webservices开发]使用XFire开发一个简单的web服务
- [转]使用C#开发一个简单的P2P应用
- ExcelServices--开发一个能够应用在Office2007客户端使用的自定义函数
- 使用servlet开发动态wap的一个简单例子
- 使用C#开发一个简单的P2P应用