当然光讲MVVM显得有些空洞,因此本文还会附带上了解经常和它搭配使用的RxJava和Retrofit。 说明:MVVM是一种基于数据驱动的架构思想,在很多平台的开发中都有应用,比如ReactNative、小程序等,RxJava是一套基于操作流的异步框架,同样对很多平台都有支持。本文仅讨论在Android平台应用开发中的使用,其它平台也相似但可能存在些...
implementation 'com.squareup.retrofit2:retrofit:2.6.2'implementation'com.squareup.retrofit2:converter-gson:2.4.0'//多了三个需要依赖的RxJavaimplementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'implementation'io.reactivex.rxjava2:rxjava:2.2.2'implementation'io.reactivex.rxjava2:rxandroid:2...
implementation'com.squareup.retrofit2:converter-gson:2.9.0'implementation'com.google.code.gson:gson:2.8.7' Retrofit创建时增加解析Factory: Retrofitretrofit=newRetrofit.Builder().baseUrl("https://devapi.qweather.com")//使用gson进行json到model的转换.addConverterFactory(GsonConverterFactory.create())...
因此还需要再做一层错误过滤,在Retrofit中,所有的异常都会抛出,并且最终由Observable的onError接收,所以我们可以自定义一个FilterSubscriber继承自Subscriber,实现onError接口,对传入的throwable参数进行判处理,代码如下 public abstract class FilterSubscriber<T> extends Subscriber<T> { public String error; @Override pub...
解决Okhttp的Response#body()#string()后Response返回体为空问题,这次废话少说,直接开始Retrofit注解参数(含@Path、@Query、@QueryMap(含文件上传下载))介绍,Retrofit通过注解的方式,进行网络请求。根据功能分类,注解可以分为:图1:Retrofit注解类型 使用Livery可以很容易的避免返回空null值无法解析的问题,统一处理《拥抱...
<--传统方式->>// 1. 创建 网络请求接口 的实例GetRequest_Interface request=retrofit.create(GetRequest_Interface.class);// 2. 采用Call<..>接口 对 发送请求 进行封装Call<Translation>call=request.getCall();// 3. 发送网络请求(异步)call.enqueue(newCallback<Translation>(){// 请求成功时回调@Overr...
1.RxJava如何与Retrofit结合 1.1 基本页面 build.gradle文件的内容 dependencies{compilefileTree(dir:'libs',include:['*.jar'])testCompile'junit:junit:4.12'compile'com.android.support:appcompat-v7:23.2.0'compile'io.reactivex:rxjava:1.1.0'compile'io.reactivex:rxandroid:1.1.0'compile'com.squareup.r...
本文是基于RxJava1.1.0和Retrofit 2.0.0-beta4来进行的。 初始化 Retrofit 新建类Api,此类就是初始化Retrofit,提供一个静态方法初始化Retrofit非常简单. 代码语言:javascript 代码运行次数:0 运行 AI代码解释 privatestaticApiServiceSERVICE;/** * 请求超时时间 ...
RetrofitCache让retrofit2+okhttp3+rxjava配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。 - yale8848/RetrofitCache
1.RxJava如何与Retrofit结合 1.1 基本页面 先扔出build.gradle文件的内容 dependencies {compilefileTree(dir:'libs', include: ['*.jar']) testCompile'junit:junit:4.12'compile'com.android.support:appcompat-v7:23.2.0'compile'io.reactivex:rxjava:1.1.0'compile'io.reactivex:rxandroid:1.1.0'compile'com...