30秒上手新一代Http请求神器RxHttp

Ok,倒计时结束!!! 到这,你已经学会了[RxHttp](()的精髓。

是的,不用怀疑,就是这么简单,使用RxHttp,任意请求,任意返回数据类型,都遵循这三个步骤,我们称之为请求三部曲,如下:

重要事情说3遍

任意请求,任意返回数据类型,皆遵循请求三部曲

任意请求,任意返回数据类型,皆遵循请求三部曲

任意请求,任意返回数据类型,皆遵循请求三部曲

gradle依赖

  • OkHttp 3.14.x以上版本, 最低要求为API 21,如你想要兼容21以下,请依赖OkHttp 3.12.x,该版本最低要求 API 9

  • asXxx方法内部是通过RxJava实现的,而RxHttp 2.2.0版本起,内部已剔除RxJava,如需使用,请自行依赖RxJava并告知RxHttp依赖的Rxjava版本

1、必选

jitpack添加到项目的build.gradle文件中,如下:

allprojects {
repositories {
maven { url “https://jitpack.io” }
}
}

注:RxHttp 2.6.0版本起,已全面从JCenter迁移至jitpack

//使用kapt依赖rxhttp-compiler时必须
apply plugin: ‘kotlin-kapt’

android {
//必须,java 8或更高
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation ‘com.github.liujingxing.rxhttp:rxhttp:2.6.0’
implementation ‘com.squareup.okhttp3:okhttp:4.9.0’ //rxhttp v2.2.2版本起,需要手动依赖okhttp
kapt ‘com.github.liujingxing.rxhttp:rxhttp-compiler:2.6.0’ //生成RxHttp类,纯Java项目,请使用annotationProcessor代替kapt
}

2、可选

android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
rxhttp_package: ‘rxhttp’, //非必须,指定RxHttp类包名
//传入你依赖的rxjava版本,可传入rxjava2、rxjava3,依赖RxJava时必须
rxhttp_rxjava: ‘rx 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》无偿开源 徽信搜索公众号【编程进阶路】 java3’

]
}
}
}
}
dependencies {
implementation ‘com.github.liujingxing.rxlife:rxlife-coroutine:2.1.0’ //管理协程生命周期,页面销毁,关闭请求

//rxjava2 (RxJava2/Rxjava3二选一,使用asXxx方法时必须)
implementation ‘io.reactivex.rxjava2:rxjava:2.2.8’
implementation ‘io.reactivex.rxjava2:rxandroid:2.1.1’
implementation ‘com.github.liujingxing.rxlife:rxlife-rxjava2:2.1.0’ //管理RxJava2生命周期,页面销毁,关闭请求

//rxjava3
implementation ‘io.reactivex.rxjava3:rxjava:3.0.6’
implementation ‘io.reactivex.rxjava3:rxandroid:3.0.0’
implementation ‘com.github.liujingxing.rxlife:rxlife-rxjava3:2.1.0’ //管理RxJava3生命周期,页面销毁,关闭请求

//非必须,根据自己需求选择 RxHttp默认内置了GsonConverter
implementation ‘com.github.liujingxing.rxhttp:converter-fastjson:2.6.0’
implementation ‘com.github.liujingxing.rxhttp:converter-jackson:2.6.0’
implementation ‘com.github.liujingxing.rxhttp:converter-moshi:2.6.0’
implementation ‘com.github.liujingxing.rxhttp:converter-protobuf:2.6.0’
implementation ‘com.github.liujingxing.rxhttp:converter-simplexml:2.6.0’
}

最后,rebuild一下(此步骤是必须的) ,就会自动生成RxHttp类

三部曲解说

到这里相信很多人已经有疑问了

  • 如果我想发送Post等其它方式请求呢?
  • 文件上传下载及进度的监听呢?
  • 我想得到自定义的数据类型呢?

这些如何通过三部曲实现呢?别着急,接下来一一为大家讲解

第一步,确定请求方式

上面例子中,我们调用了RxHttp.get("http://...")语句,其中get操作符就代表Get请求。由此,我们可以猜测,发送Post请求,只需要调用post操作符即可。然而我们只猜对了一半,为啥这么说呢?Post请求中,我们常见的又分为两种,一种的表单形式的Post,另一种是Json字符串形式的Post。为此,[RxHttp](()提供了两个发送Post请求的操作符,分别是postFormpostJosn,此时,我们就可以这样发送Post请求

RxHttp.postForm(“http://…”) //发送表单形式的Post请求
.asString() //返回String类型
.subscribe(s -> { //订阅观察者,
//请求成功
}, throwable -> {
//请求失败
});

RxHttp.postJson(“http://…”) //发送Json字符串单形式的Post请求
.asString() //返回String类型
.subscribe(s -> { //订阅观察者,
//请求成功
}, throwable -> {
//请求失败
//返回String类型
.subscribe(s -> { //订阅观察者,
//请求成功
}, throwable -> {
//请求失败

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值