Retrofit结合Rxjava封装思路:
- 主要分为四个部分:实体类 ,Observable ,Subscriber,Retrofit。
实体类
一般客户端都是通过链接从服务端获取数据,然后再注入到应用中,从而更新UI。返回的数据一般为JSON。
{
items: [],
has_more: true,
quota_max: 300,
quota_remaining: 299
}
常见结构就是如此,对于我们个人而言呢有用的仅仅是tems部分,而且后期需求变化的话,items里的json数据结构会发生改变的,对应的类结构会发生改变,所以用泛型来解决items类多变的问题。把items中用到的类单独封装。
public class StackUserEntity<T> {
/**
* items : [{"reputation":904115,"creation_date":1222430705,"user_type":"registered","user_id":22656,"accept_rate":86,"location":"Reading, United Kingdom","website_url":"http://csharpindepth.com","link":"http://stackoverflow.com/users/22656/jon-skeet","profile_image":"https://www.gravatar.com/avatar/6d8ebb117e8d83d74ea95fbdd0f87e13?s=128&d=identicon&r=PG","display_name":"Jon Skeet"}]
* has_more : true
* quota_max : 300
* quota_remaining : 299
*/
private boolean has_more;
private int quota_max;
private int quota_remaining;
private T items;
}
Observable<StackUserEntity<List<ItemsBean>>>
Retrofit
此类更多封装的时关于Retrofit的配置及请求过程,并不涉及服务接口。以单例模式封装。
public class HttpMethodes{
public static final String baseUrl = "https://api.douban.com/v2/movie/";
private static final int DEFAULT_TIMEOUT = 5;
private Retrofit retrofit;
private static HttpMethodes httpMethodes;
//构造函数私有化
private HttpMethodes() {
OkHttpClient.Builder builder =new OkHttpClient.Builder();
builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(builder.build())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
//懒汉式单例,并加入线程锁,使得线程安全(不会因为多线程调用时创造出两个实例)
public static HttpMethodes getInstance() {
if (httpMethodes == null) {
synchronized (HttpMethodes.class) {
if (httpMethodes == null) {
httpMethodes = new HttpMethodes();
}
}
}
return httpMethodes;
}
//这里是把线程的调度封装了,避免重复写。
public <T> void getMovie(Subscriber<T> subscriber,Observable<T> observable) {
toSubscriber(observable,subscriber);
}
//因为是单例,服务接口的对象创建和url的参数是不能写死的。这里也用泛型。
public <T> T toService(final Class<T> from) {
return retrofit.create(from);
}
private void toSubscriber(Observable o , Subscriber s) {
o.subscribeOn(Schedulers.io())
.unsubscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(s);
}
}
Observable
Observable这部分更多的是封装Retrofit的服务接口参数传递即事件的处理。因这部分为变化的不可能在Retrofit单例那里封装。
private Observable<List<ItemsBean>> getSource() {
return methodes.toService(StackExchangeService.class)
.getSource(10) //此处使用map操作符呢是为了提取StackUserEntity实体类部分的List<ItemsBean>;
.map(new Func1<StackUserEntity<List<ItemsBean>>, List<ItemsBean>>() {
@Override
public List<ItemsBean> call(StackUserEntity<List<ItemsBean>> listStackUserEntity) {
return listStackUserEntity.getItems();
}
});
}
Subscriber
创建一个继承Subscriber的抽象类,把OnNext( )定义为抽象方法,因为OnNext()方法中,更多的传递数据给UI使用。
public abstract class ProgressSubscriber<T> extends Subscriber<T> {
private SubscriberOnNextListener mSubscriber;
@Inject
public Context context;
private ProgressDialog dialog;
public void setmSubscriber(SubscriberOnNextListener mSubscriber) {
this.mSubscriber = mSubscriber;
}
public void setContext(Context context) {
this.context = context;
}
@Override
public void onStart() {
showProgressDialog();
super.onStart();
}
@Override
public void onCompleted() {
destroyProgress();
Toast.makeText(context, "当前任务已经完成", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable e) {
destroyProgress();
Toast.makeText(context, "当前任务出现了错误,请稍候重试", Toast.LENGTH_SHORT).show();
}
//可以根据需求写入正确的类型
@Override
abstract public void onNext(T t);
private void destroyProgress() {
if (dialog!=null) {
dialog.dismiss();
}
dialog = null;
}
private void showProgressDialog() {
if (dialog == null) {
dialog = new ProgressDialog(context);
dialog.setCancelable(true);
dialog.setMessage("正在加载中,请稍候!");
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (! isUnsubscribed()) {
unsubscribe(); //当加载框,加载完成时取消对Observable的订阅(即关闭url连接。)
}
}
});
}
if (! dialog.isShowing()) {
dialog.show();
}
}
}