RxAndroid的使用方式5-RxVolley

RxVolley使用指南

RxVolley 项目地址: https://github.com/kymjs/RxVolley

概述

RxVolley是一个基于Volley的网络请求库;
同时支持RxJava;
可以选择使用OKHttp替代默认的 HttpUrlConnection 做网络请求;
可以选择使用图片加载功能(复用的网络请求将有效减少apk体积);
移除了原Volley的 HttpClient 相关 API ,可在 API23 环境编译;
内置了RxBus的实现,可有效替换掉EventBus等相关库;

依赖

使用RxVolley,需要在你的build.gradle文件中加入
{% highlight null %}
compile ‘com.kymjs.rxvolley:rxvolley:1.0.7’

{% endhighlight %}
如果你还想使用OKhttp来替代默认的HttpUrlconnection,需要加入
{% highlight null %}
compile ‘com.kymjs.rxvolley:okhttp:1.0.7’

{% endhighlight %}
如果你想使用RxVolley的图片加载功能(复用http模块可以有效减少apk大小),需要加入
{% highlight null %}
compile ‘com.kymjs.rxvolley:bitmapcore:1.0.7’
{% endhighlight %}
官网文档:
http://rxvolley.mydoc.io/

完整的使用示例

{% highlight java %}
public class VolleyActivity extends RxAppCompatActivity {

//ButterKnife控件查找
@BindView(R.id.textView)
TextView mTvText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_simple);
    ButterKnife.bind(this);

    new RxVolley.Builder()
            .url("https://jackwolf1.github.io/")//设置url
            .httpMethod(RxVolley.Method.GET)//设置连接方法
            .contentType(RxVolley.ContentType.FORM)//设置内容类型
            .getResult()//获取数据
            .map(result -> new String(result.data))//数据转换
            .subscribeOn(Schedulers.io())//开启线程
            .observeOn(AndroidSchedulers.mainThread())//主线程
            .compose(bindToLifecycle()) // 管理生命周期, 防止内存泄露
            .subscribe(result -> {
                //结果显示
                mTvText.setText(result);
            });
}

@Override
protected void onDestroy() {
    super.onDestroy();

}

}
{% endhighlight %}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值