android novate乱码,Android RxJava+Retrofit2+RxBinding

Android RxJava+Retrofit2+RxBinding

本文原创,转载请注明出处。欢迎关注我的 简书。

安利一波我写的开发框架:MyScFrame喜欢的话就给个Star

前言:

之前写了一篇名为Android 手把手教你使用Retrofit2的文章,只是单纯并简单的介绍了下Retrofit2的使用,现在我们把它跟RxJava以及RxBinding关联起来,看看效果如何

添加依赖

RxBinding的依赖比较麻烦,是按控件包区分的,我这边只用到了基础包也就是android.widget.所以只引入了compile 'com.jakewharton.rxbinding:rxbinding:1.0.0'

//全新网络请求框架Rxjava+RxAndroid+ReTrofit2+okHttp3

compile 'com.tamic.novate:novate:1.2.7'

compile 'com.squareup.retrofit2:retrofit:2.1.0'

compile 'com.squareup.retrofit2:converter-gson:2.1.0'

compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

//日志拦截器

compile 'com.squareup.okhttp3:logging-interceptor:3.5.0'

compile 'io.reactivex:rxjava:1.2.4'

compile 'io.reactivex:rxandroid:1.2.1'

compile 'org.ligboy.retrofit2:converter-fastjson-android:2.1.0'

//RxBinding

compile 'com.jakewharton.rxbinding:rxbinding:1.0.0'

// compile 'com.jakewharton.rxbinding:rxbinding-support-v4:1.0.0'

// compile 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:1.0.0'

// compile 'com.jakewharton.rxbinding:rxbinding-design:1.0.0'

// compile 'com.jakewharton.rxbinding:rxbinding-recyclerview-v7:1.0.0'

开始关联

MyRetrofit2Service差异

在使用RxJava之前是这样的

@POST()

Call> post(@Url String url, @QueryMap Map map);

使用了RxJava之后是这样的

@POST()

Observable> rxPost(@Url String url, @QueryMap Map map);

MyRetrofit2调用方法上的差异

在使用RxJava之前是这样的

/**

* 异步调用

*/

public void getEnqueue() {

Call> call = mApi.post(NEWS_URI, params);

call.enqueue(new Callback>() {

@Override

public void onResponse(Call> call, Response> response) {

//处理请求成功

Log.e("OkHttp", "处理成功请求 response = " + response.body().toString());

}

@Override

public void onFailure(Call> call, Throwable t) {

//处理请求失败

Log.e("OkHttp", "处理失败请求");

}

});

// cancelCall(call);

}

使用了RxJava之后是这样的

public void RxJavaGetEnqueue(Subscriber subscriber) {

Observable observable = mApi.rxPost(NEWS_URI, params)

.map(new HttpResultFunc());

toSubscribe(observable, subscriber);

}

private void toSubscribe(Observable o, Subscriber s) {

o.subscribeOn(Schedulers.io())//设置事件触发在非主线程

.unsubscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())//在主线程处理返回数据

.subscribe(s);

}

/**

* 用来统一处理Http的resultCode,并将HttpResult的Data部分剥离出来返回给subscriber

*

* @param Subscriber真正需要的数据类型,也就是Data部分的数据类型

*/

private class HttpResultFunc implements Func1, T> {

@Override

public T call(HttpResult httpResult) {

Log.e("OkHttp", "HttpResultFunc httpResult = " + httpResult.toString());

if ("200".equals(httpResult.getStatus_code()) && httpResult.isSuccess()) {

return httpResult.getData();

}

throw new ApiException(httpResult.getStatus_code());

}

}

初看代码量变多了,没错,是变多了,但是谁叫他最近比较火呢,而且目前我只是实现比较简单的场景,还没发挥出RxJava的真正功效呢.

这里需要说下HttpResultFunc方法的作用,好用就好用在这里,这里我是根据网络请求的code跟success两个参数判断是否是成功的,如果是成功返回,那就返回data,如果是错误的话,自己写了个ApiException类是用来处理错误信息的

/**

* Created by caihan on 2017/1/14.

*/

public class ApiException extends RuntimeException {

public static final int WRONG_1 = 100;

public static final int WRONG_2 = 101;

public ApiException(int resultCode) {

this(getApiExceptionMessage(resultCode));

}

public ApiException(String detailMessage) {

super(detailMessage);

}

/**

* 由于服务器传递过来的错误信息直接给用户看的话,用户未必能够理解

* 需要根据错误码对错误信息进行一个转换,在显示给用户

*

* @param code

* @return

*/

private static String getApiExceptionMessage(int code) {

String message = "";

switch (code) {

case WRONG_1:

message = "错误1";

break;

case WRONG_2:

message = "错误2";

break;

default:

message = "未知错误";

}

return message;

}

}

界面上调用方式的变化

之前是这样的

MyRetrofit2.getInstance().getEnqueue();

现在是这样子的

private void rxGet() {

MyRetrofit2.getInstance().RxJavaGetEnqueue(new Subscriber() {

@Override

public void onCompleted() {

Log.e("OkHttp", "rxGet onCompleted");

}

@Override

public void onError(java.lang.Throwable e) {

Log.e("OkHttp", "rxGet onError e= " + e.toString());

}

@Override

public void onNext(News news) {

Log.e("OkHttp", "rxGet onNext news= " + news.toString());

}

});

}

用RxBinding处理频繁点击问题

开发中大家都会遇到这类问题,用户不(gui)小(cai)心(xin)点击了N次Button,导致Button相应了N次,之前我是写了个DoubleClick类,专门处理此类事件,相信大家也都会,现在有了RxBinding,解放了

RxView.clicks(mButton)

.throttleFirst(1, TimeUnit.SECONDS)

.subscribe(new Action1() {

@Override

public void call(Void o) {

Log.d("OkHttp", "clickAction call");

rxGet();

}

});

throttleFirst()方法就是处理这种不小心事件,它会把设置时间内的其他响应事件过滤掉,怎么样,是不是很好用呀...其他的控件如何处理我这边就不写了,大家感兴趣的话可以看看相关资料

资料

总结

响应式开发我也还在摸索当中,这里没做过多的封装,因为还有很多地方没搞懂,最近也是一直在找相关的资料.如果大家在其他地方看到了一些相关的优秀文章,可以分享给我,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值