RxVolley使用指南

RxVolley 项目地址: https://github.com/kymjs/RxVolley
# 概述 # RxVolley是一个基于Volley的网络请求库; 同时支持RxJava; 可以选择使用OKHttp替代默认的 HttpUrlConnection 做网络请求; 可以选择使用图片加载功能(复用的网络请求将有效减少apk体积); 移除了原Volley的 HttpClient 相关 API ,可在 API23 环境编译; 内置了RxBus的实现,可有效替换掉EventBus等相关库;
# 依赖 # 使用RxVolley,需要在你的build.gradle文件中加入

    compile 'com.kymjs.rxvolley:rxvolley:1.0.7'

如果你还想使用OKhttp来替代默认的HttpUrlconnection,需要加入

    compile 'com.kymjs.rxvolley:okhttp:1.0.7'

如果你想使用RxVolley的图片加载功能(复用http模块可以有效减少apk大小),需要加入

    compile 'com.kymjs.rxvolley:bitmapcore:1.0.7'

官网文档: http://rxvolley.mydoc.io/

完整的使用示例

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();

    }
}