This is a simple http library based on Retrofit and RxJava 这是一个基于Retrofit和RxJava封装的网络请求库,依赖使用都非常的简单方便。 持续更新中。。。
implementation 'com.bus.tvm:mok:0.0.1'
Mok.init(
InitialConfig.Builder()
.baseUrl("https://www.wanandroid.com") //requested
.connectionTimeOut(60L) //optional default 60s
.build()
)
- baseUrl必需字段
- connectionTimeOut非必须字段,默认60秒
Mok.get<String>("http://www.baidu.com")
.converter(StringConverter())//类型转换器
.execute()
.subscribe(object : Observer<String> {
override fun onComplete() {
}
override fun onSubscribe(d: Disposable) {
}
override fun onNext(t: String) {
Log.d("TAG", t)
}
override fun onError(e: Throwable) {
}
})
Mok.post<WxArticleList>("wxarticle/chapters/json")
.converter(EntityConverter(WxArticleList::class.java)) //data type converter
.connectionTimeOut(30L) //optional default Mok initial config
.execute() //create observable
.subscribe(object : Observer<WxArticleList> {
override fun onComplete() {
}
override fun onSubscribe(d: Disposable) {
}
override fun onNext(t: WxArticleList) {
Log.d("TAG", t.toString())
}
override fun onError(e: Throwable) {
}
})
- 发起网络请求时需要手动指定返回值类型,如果返回值是实体对象,需要使用EntityConverter,如果返回值是String,需要使用StringConverter
- 超时时间如果不指定,会使用全局的超时时间
- execute调用产生被观察者对象,然后subscribe订阅,走RxJava流程