RxAndroid

来源:互联网 发布:mac水漾轻盈粉底液 编辑:程序博客网 时间:2024/05/16 19:49

       RxAndroid 应用(一)(简易天气App)

       RxAndroid 应用(二)(简易天气App)

       RxAndroid 应用(三)(简易天气App之Observable内存泄露)

       RxAndroid 应用(四)(简易天气App之RxBus)

     

      关于RxJava、RxAndroid 详解请看 RxJava、RxAndroid详解

      关于MVP的详解请移步 Android应用架构之Android MVP使用

      关于Retrofit的使用介绍请移步Android应用架构之Retrofit、RxAndroid使用 

      关于GreenDAO的使用

今天开始我主要应用RxAndroid、MVP、Retrofit实现一个简单的app,以此来介绍RxAndroid的使用。

首先看看需要实现简易天气App(只是为了介绍如何使用RxJava在Android实际项目中的应用)的效果:

主页内容:



右侧栏天气列表:



左侧栏城市列表



首先看看Activity主要代码(使用MVP模式):

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //调用Presenter的方法获取数据  
  2. mMainPresenter = new MainPresenterImpl(this);  
  3. mMainPresenter.getPlaceData();  
  4. mMainPresenter.getWeatherData("成都");  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //显示主页和右侧栏天气数据  
  2. public void setupWeatherData(WeatherResponse weatherResponse) {  
  3.     if (weatherResponse == nullreturn;  
  4.     setTitleText(DateUtils.getWeekDay(weatherResponse.date));  
  5.     if (weatherResponse.results != null && weatherResponse.results.size() > 0) {  
  6.         WeatherResult result = weatherResponse.results.get(0);  
  7.         mTvCity.setText(getString(R.string.city, result.currentCity));  
  8.         mTvPm25.setText(getString(R.string.pm25, result.pm25));  
  9.   
  10.         mWeatherDataAdapter.setData(result.weather_data);  
  11.         mWeatherDataAdapter.notifyDataSetChanged();  
  12.   
  13.         mWeatherExtraAdapter.setData(result.index);  
  14.         mWeatherExtraAdapter.notifyDataSetChanged();  
  15.     }  
  16. }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //显示左侧栏城市列表  
  2. @Override  
  3. public void setupPlaceData(List<Place> placeList) {  
  4.     if (placeList == null) {  
  5.         return;  
  6.     }  
  7.     mPlaceAdapter.setData(placeList);  
  8.     mPlaceAdapter.notifyDataSetChanged();  
  9. }  

接下来看看如何在Presenter中应用RxJava、RxAndroid获取数据

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //获取天气数据  
  2. @Override  
  3. public void getWeatherData(String place) {  
  4.     if (TextUtils.isEmpty(place)) {  
  5.         return;  
  6.     }  
  7.     mMainView.showProgress();  
  8.     ServiceManager.getInstance().getApiService().getWeatherInfo(place, Constants.BAIDU_AK)  
  9.             .subscribeOn(Schedulers.io())  
  10.             .observeOn(AndroidSchedulers.mainThread())  
  11.             .subscribe(new Subscriber<WeatherResponse>() {  
  12.                 @Override  
  13.                 public void onCompleted() {  
  14.                     Log.e(TAG, "onCompleted");  
  15.                     mMainView.hideProgress();  
  16.                 }  
  17.   
  18.                 @Override  
  19.                 public void onError(Throwable e) {  
  20.                     Log.e(TAG, e.getMessage(), e);  
  21.                     mMainView.hideProgress();  
  22.                 }  
  23.   
  24.                 @Override  
  25.                 public void onNext(WeatherResponse weatherResponse) {  
  26.                     mMainView.setupWeatherData(weatherResponse);  
  27.                 }  
  28.             });  
  29. }  



[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface ApiService {  
  2.   
  3.     /*@GET("service/getIpInfo.php") 
  4.     Call<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);*/  
  5.   
  6.     @GET("service/getIpInfo.php")  
  7.     Observable<GetIpInfoResponse> getIpInfo(@Query("ip") String ip);  
  8.   
  9.   
  10.     //http://api.map.baidu.com/telematics/v3/weather?location=%E6%88%90%E9%83%BD&output=json&ak=MPDgj92wUYvRmyaUdQs1XwCf  
  11.     @GET("/telematics/v3/weather?output=json")  
  12.     Observable<WeatherResponse> getWeatherInfo(@Query("location") String location, @Query("ak") String ak);  
  13. }  

如上所述,我们通过百度api获取天气数据使用的是Retrofit框架,它能自动的返回Observable对象。

那么我们如何通过RxJava获取本地文件中的城市列表呢?(为了方便演示,我将城市列表作为一个json字符串放于文件中)

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @Override  
  2. public void getPlaceData() {  
  3.     PlaceRepository repository = new PlaceRepository();  
  4.     repository.getPlaceList(BaseApplication.getInstance())  
  5.             .subscribeOn(Schedulers.io())  
  6.             .observeOn(AndroidSchedulers.mainThread())  
  7.             .subscribe(new Subscriber<List<Place>>() {  
  8.                 @Override  
  9.                 public void onNext(List<Place> places) {  
  10.                     mMainView.setupPlaceData(places);  
  11.                 }  
  12.   
  13.                 @Override  
  14.                 public void onCompleted() {  
  15.   
  16.                 }  
  17.   
  18.                 @Override  
  19.                 public void onError(Throwable e) {  
  20.   
  21.                 }  
  22.             });  
  23. }  

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class PlaceRepository {  
  2.   
  3.     public Observable<List<Place>> getPlaceList(final Context context) {  
  4.         return Observable.create(new Observable.OnSubscribe<List<Place>>() {  
  5.             @Override  
  6.             public void call(Subscriber<? super List<Place>> subscriber) {  
  7.                 try {  
  8.                     AssetManager assertManager = context.getAssets();  
  9.                     InputStream inputStream = assertManager.open("place");  
  10.                     ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  11.                     byte[] data = new byte[1024];  
  12.                     int count = -1;  
  13.                     while((count = inputStream.read(data,01024)) != -1) {  
  14.                         outStream.write(data, 0, count);  
  15.                     }  
  16.                     String json =  new String(outStream.toByteArray(),"UTF-8");  
  17.                     Gson gson = new GsonBuilder().create();  
  18.                     List<Place> placeList = gson.fromJson(json, new TypeToken<List<Place>>() {}.getType());  
  19.                     subscriber.onNext(placeList);  
  20.                 } catch (Exception e) {  
  21.                     subscriber.onError(e);  
  22.                 }  
  23.                 subscriber.onCompleted();  
  24.             }  
  25.         });  
  26.     }  
  27. }  
通过上述代码,我们就能完成界面所示功能了,是不是省去了Handler callback,new Thread()这些操作了,这就为什么说RxJava是用来解决Callback Hell的。

这里的代码会看起来不是那么好的地方:在Activity中分别调用了获取天气数据和城市列表的方法,那么问题来了,如果取数据的时候显示了process Dialog, 我该在什么时候结束呢,写flag判断?请看下章博文分解。

代码地址:https://github.com/mickyliu945/CommonProj

0 0
原创粉丝点击