前言
之前做网络请求,用的是android-async-http,基于HttpClient 的,虽然早已淘汰,但一直懒得换,RxJava+Retrofit 是完美搭配,所以下定决定重构一下现在的项目
网上查了一些资料,遇到了一些小坎坷,终于搞定了,因为网上查到的一些文章大多都是半年以前的,而我使用的都是最新的库,遇到了一些新的问题,所以感觉有必要写篇文章帮助后人少走一些弯路
本文默认读者对RxJava和Retrofit 已经有了一定的了解,若对RxJava和Retrofit 还不了解,请先查阅相关资料
使用
1、添加依赖库
compile "io.reactivex.rxjava2:rxjava:2.1.1"compile 'io.reactivex.rxjava2:rxandroid:2.0.1'compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
converter-gson是Retrofit到Gson进行转换的库,adapter-rxjava2是Retrofit到RxJava进行转换的库
这里我是采用Google Gson进行数据解析的,如果你使用的是Jackson,替换为如下依赖即可
compile 'com.squareup.retrofit2:converter-jackson:2.3.0'
如果需要添加HttpLoggingInterceptor进行调试,添加如下依赖
compile 'com.squareup.okhttp3:logging-interceptor:3.8.1'
2、写一个Service
public interface RetrofitService { @FormUrlEncoded @POST("account/login") Observable<BaseEntity<UserInfo>> login( @Field("userId") String userId, @Field("password") String password ); @GET("video/getUrl") Observable<BaseEntity<VideoUrl>> getVideoUrl( @Query("id") long id ); @FormUrlEncoded @POST("user/addVideo") Observable<BaseEntity<Boolean>> addVideo( @FieldMap Map<String, Object> map ); }
相对于单独使用Retrofit,该处返回的是Observable对象
3、通常服务器端会返回统一的数据格式,这里我们写一个BaseEntity
public class BaseEntity<E> { @SerializedName("code") private int code; @SerializedName("msg") private String msg; @SerializedName("data") private E data; public boolean isSuccess() { return code == 0; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public E getData() { return data; } public void setData(E data) { this.data = data; } }
4、然后我们可以封装一个RetrofitFactory
public class RetrofitFactory { private static final String BASE_URL = "http://api.baidu.com/"; private static final long TIMEOUT = 30; // Retrofit是基于OkHttpClient的,可以创建一个OkHttpClient进行一些配置 private static OkHttpClient httpClient = new OkHttpClient.Builder() // 添加通用的Header .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request.Builder builder = chain.request().newBuilder(); builder.addHeader("token", "123"); return chain.proceed(builder.build()); } }) /* 这里可以添加一个HttpLoggingInterceptor,因为Retrofit封装好了从Http请求到解析, 出了bug很难找出来问题,添加HttpLoggingInterceptor拦截器方便调试接口 */ .addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() { @Override public void log(String message) { } }).setLevel(HttpLoggingInterceptor.Level.BASIC)) .connectTimeout(TIMEOUT, TimeUnit.SECONDS) .readTimeout(TIMEOUT, TimeUnit.SECONDS) .build(); private static RetrofitService retrofitService = new Retrofit.Builder() .baseUrl(BASE_URL) // 添加Gson转换器 .addConverterFactory(GsonConverterFactory.create(buildGson())) // 添加Retrofit到RxJava的转换器 .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(httpClient) .build() .create(RetrofitService.class); public static RetrofitService getInstance() { return retrofitService; } private static Gson buildGson() { return new GsonBuilder() .serializeNulls() .setFieldNamingPolicy(FieldNamingPolicy.IDENTITY) // 此处可以添加Gson 自定义TypeAdapter .registerTypeAdapter(UserInfo.class, new UserInfoTypeAdapter()) .create(); } }
5、通常我们会在IO线程进行请求,在主线程进行回调
public class RxSchedulers { public static <T> ObservableTransformer<T, T> compose() { return new ObservableTransformer<T, T>() { @Override public ObservableSource<T> apply(Observable<T> observable) { return observable .subscribeOn(Schedulers.io()) .doOnSubscribe(new Consumer<Disposable>() { @Override public void accept(Disposable disposable) throws Exception { if (!Utils.isNetworkConnected()) { Toast.makeText(context, R.string.toast_network_error, Toast.LENGTH_SHORT).show(); } } }) .observeOn(AndroidSchedulers.mainThread()); } }; } }
这里我们可以添加一个通用的网络连接判断
6、RxJava Observable 订阅需要传入一个Observer对象,此处封装一个BaseObserver
public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> { private static final String TAG = "BaseObserver"; private Context mContext; protected BaseObserver(Context context) { this.mContext = context.getApplicationContext(); } @Override public void onSubscribe(Disposable d) { } @Override public void onNext(BaseEntity<T> value) { if (value.isSuccess()) { T t = value.getData(); onHandleSuccess(t); } else { onHandleError(value.getMsg()); } } @Override public void onError(Throwable e) { Log.e(TAG, "error:" + e.toString()); } @Override public void onComplete() { Log.d(TAG, "onComplete"); } protected abstract void onHandleSuccess(T t); protected void onHandleError(String msg) { Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); } }
7、调用
private void login(String userId, String password) { Observable<BaseEntity<UserInfo>> observable = RetrofitFactory.getInstance().login(userId, password); observable.compose(RxSchedulers.compose()).subscribe(new BaseObserver<UserInfo>(context) { @Override protected void onHandleSuccess(UserInfo userInfo) { // 保存用户信息等操作 } }); }
RxJava生命周期管理
可以用RxLifecycle来管理RxJava的生命周期
RxLifecycle:https://github.com/trello/RxLifecycle/tree/2.x