当前位置:   article > 正文

RxJava+Retrofit网络请求框架封装实现MVVM模式_java maven rxjava retrofit

java maven rxjava retrofit

RxJava的作用主要就是负责线程调度,所以就可以把Retrofit封装为一个工具类;主要代码如下:

  1. import android.support.annotation.NonNull;
  2. import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
  3. import java.util.concurrent.TimeUnit;
  4. import okhttp3.OkHttpClient;
  5. import retrofit.ApiUrl;
  6. import retrofit.Constans;
  7. import retrofit2.Retrofit;
  8. import retrofit2.converter.gson.GsonConverterFactory;
  9. /**
  10. * Retrofit封装
  11. */
  12. public class RetrofitUtils {
  13. private static final String TAG = "RetrofitUtils";
  14. private static ApiUrl mApiUrl;
  15. /**
  16. * 单例模式
  17. */
  18. // 主要就是调用这个方法获得Retrofit的接口服务对象
  19. public static ApiUrl getApiUrl() {
  20. if (mApiUrl == null) {
  21. synchronized (RetrofitUtils.class) {
  22. if (mApiUrl == null) {
  23. mApiUrl = new RetrofitUtils().getRetrofit();
  24. }
  25. }
  26. }
  27. return mApiUrl;
  28. }
  29. private RetrofitUtils(){}
  30. public ApiUrl getRetrofit() {
  31. // 初始化Retrofit
  32. ApiUrl apiUrl = initRetrofit(initOkHttp()) .create(ApiUrl.class);
  33. return apiUrl;
  34. }
  35. /**
  36. * 初始化Retrofit
  37. */
  38. @NonNull
  39. private Retrofit initRetrofit(OkHttpClient client) {
  40. return new Retrofit.Builder()
  41. .client(client)
  42. .baseUrl(Constans.BaseUrl)
  43. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  44. .addConverterFactory(GsonConverterFactory.create())
  45. .build();
  46. }
  47. /**
  48. * 初始化okhttp
  49. */
  50. @NonNull
  51. private OkHttpClient initOkHttp() {
  52. return new OkHttpClient().newBuilder()
  53. .readTimeout(Constans.DEFAULT_TIME, TimeUnit.SECONDS)//设置读取超时时间
  54. .connectTimeout(Constans.DEFAULT_TIME, TimeUnit.SECONDS)//设置请求超时时间
  55. .writeTimeout(Constans.DEFAULT_TIME,TimeUnit.SECONDS)//设置写入超时时间
  56. .addInterceptor(new LogInterceptor())//添加打印拦截器
  57. .retryOnConnectionFailure(true)//设置出现错误进行重新连接。
  58. .build();
  59. }
  60. }

定义Retrofit的服务接口:

  1. public interface WeatherApi {
  2. @GET("101210101.html")
  3. Single<Weather> getWeather();
  4. }

在ViewModel中定义网络访问服务器接口的方法

  1. public void getWeather2(){
  2. RetrofitUtils
  3. .getApiService()
  4. .getWeather()
  5. .subscribeOn(Schedulers.io())
  6. .observeOn(AndroidSchedulers.mainThread())
  7. .subscribe(weather -> weatherMutableLiveData.setValue(weather.getWeatherinfo()));
  8. }

在Activity/Fragment中给ViewModel中的LiveData添加观察者,在需要获取网络数据的地方通过ViewModel对象调用getWeather()方法 weatherVM.getWeather2()即可,然后获取网络数据成功后LiveData就会通知数据改变,然后dataBinding框架就会自动刷新页面.

添加观察者:

  1. //LiveData的observer为这个activity,只有当activity还存活的时候才会收到数据更新的通知
  2. weatherVM.getWeatherMutableLiveData().observe(this,this::updata);
  3. //在观察者数据更新回调中调用这个方法告诉DataBinding数据更新以刷新UI页面
  4. private void updata(Weather.WeatherinfoBean weatherinfoBean) {
  5. activityMainBinding.setWeatherinfo(weatherinfoBean);
  6. }

通过DataBinding绑定天气数据实体类WeatherInfoBean, 这样就实现了一个简单的MVVM模式架构.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/300871
推荐阅读
相关标签
  

闽ICP备14008679号