RxNetWork

Project Url: 7449/RxNetWork
Introduction: rxjava1.x and retrofit2.x 版本的网络请求简化库,以及 RxBus
More: Author   ReportBugs   DemoAPK   
Tags:
RxJava-NetWork-retrofit2-RxBus-okhttp-gson-

this is android network ,and RxBus

android 网络请求简化库

RxNetWork 项目试用:

https://github.com/7449/ZLSimple

RxJsoupNetWork 项目试用

https://github.com/7449/JsoupSample

implementation 'com.ydevelop:rxNetWork:0.1.93'

implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'

// json
implementation 'com.squareup.retrofit2:converter-jackson:2.5.0'

// 如果使用`RxCache`,则使用`gson`
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

建议初始化:

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        RxNetWork.initialization(SimpleRxNetOptionFactory(BASE_API, JacksonConverterFactory.create()))
        RxCache
                .instance
                .setDiskBuilder(RxCache.DiskBuilder(FileUtils.getDiskCacheDir(context, "RxCache")))
    }
}

支持以下自定义(如果不想使用内置类库可自定义):

OkHttpClient

Converter.Factory

CallAdapter.Factory

使用方法:

这里假设在Application里已经自定义好BaseUrl;

参数一: tag,用于取消网络请求 参数二:observable 参数三: 回调,网络请求开始,异常,结束,成功之类的状态

  • 取消网络请求:

      RxNetWork.instance.cancel()
    
    RxNetWork.instance.getApi(tag,
            RxNetWork.observable(),
            object : RxNetWorkListener<Any> {
                override fun onNetWorkStart() {
                }

                override fun onNetWorkError(e: Throwable) {
                }

                override fun onNetWorkComplete() {
                }

                override fun onNetWorkSuccess(data: Any) {
                }
            })

RxCache

okhttp不支持 post 缓存,所以新增RxCache对缓存的支持

  • 只走网络

    Observable

            .compose(RxCache.getInstance().<T>transformerN())
    
  • 走缓存

true : 有网的情况下 网络优先,否则 缓存优先

Observable
          .compose(RxCache.getInstance().transformerCN("", true, new TypeToken<Any>() {
                                  }))

配置 Header:

如果使用默认的okhttp,配置 Header 需要如下操作:

    RxNetWork.initialization(object : SimpleRxNetOptionFactory(URL, JacksonConverterFactory.create()) {
        override val headerInterceptor: Interceptor? = SimpleHeaderInterceptor()
    })

配置 Log

如果使用默认的okhttp,配置 Log 需要如下操作:

    RxNetWork.initialization(object : SimpleRxNetOptionFactory(URL, JacksonConverterFactory.create()) {
        override val logInterceptor: Interceptor? = SimpleLogInterceptor()
    })

RxBus 使用:

发送消息:

    RxBus.instance.post(tag,message)
    RxBus.instance.post(tag)

注册消息体:

    RxBus.instance
            .register(tag,object :RxBusCallBack<Any>{
                override fun onBusError(throwable: Throwable) {
                }

                override fun busOfType(): Class<Any> {
                }

                override fun onBusNext(entity: Any) {
                }
            })

解绑:

RxBus.instance.unregister(tag)

License

Copyright (C) 2017 yuebigmeow@gamil.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea