RxHttp-完美适配Android-10-11-上传-下载-进度监听

文章介绍了使用RxHttp进行HTTP请求,包括如何在gradle中配置依赖,以及在Android10/11环境下处理文件上传和下载,特别是利用协程和Uri对象的处理方式。同时提及了Android10分区存储的新要求和应对策略。
摘要由CSDN通过智能技术生成

RxHttp 2000+star,协程请求,仅需三步

RxHttp 让你眼前一亮的Http请求框架

gradle依赖

//使用kapt依赖rxhttp-compiler,需要导入kapt插件
apply plugin: ‘kotlin-kapt’

android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
//必须,告知RxHttp你依赖的okhttp版本,目前已适配 v3.12.0 - v4.9.0版本 (v4.3.0除外)
rxhttp_okhttp: ‘4.9.0’,
//使用asXxx方法时必须,告知RxHttp你依赖的rxjava版本,可传入rxjava2、rxjava3
rxhttp_rxjava: ‘rxjava3’,
rxhttp_package: ‘rxhttp’ //非必须,指定RxHttp类包名
]
}
}
}
//必须,java 8或更高
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
//以下3个为必须,
implementation ‘com.ljx.rxhttp:rxhttp:2.4.1’
implementation ‘com.squareup.okhttp3:okhttp:4.9.0’ //rxhttp v2.2.2版本起,需要手动依赖okhttp
kapt ‘com.ljx.rxhttp:rxhttp-compiler:2.4.1’ //生成RxHttp类,纯Java项目,请使用annotationProcessor代替kapt

implementation ‘com.ljx.rxlife:rxlife-coroutine:2.0.1’ //管理协程生命周期,页面销毁,关闭请求

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

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

//非必须,根据自己需求选择 RxHttp默认内置了GsonConverter
implementation ‘com.ljx.rxhttp:converter-fastjson:2.4.1’
implementation ‘com.ljx.rxhttp:converter-jackson:2.4.1’
implementation ‘com.ljx.rxhttp:converter-moshi:2.4.1’
implementation ‘com.ljx.rxhttp:converter-protobuf:2.4.1’
implementation ‘com.ljx.rxhttp:converter-simplexml:2.4.1’
}
复制代码

2、Android 10/11 分区存储

当我们App的targetSdkVersion更改为28以上,并且运行在Android 10以上设备时,我们无法再以绝对路径的方式,去读写非沙盒目录下的文件,当然,如果App是覆盖安装(如:targetSdkVersion 28 覆盖安装为 29),则会保持原来的访问方式。

requestLegacyExternalStorage属性

如果我们的app将targetSdkVersion更改为28以上,且想保持原来的访问方式,则需要在清单文件中将 requestLegacyExternalStorage 的值设置为 true,如下:

<manifest …>

<application android:requestLegacyExternalStorage=“true” … >



复制代码

此时,便可继续以原来的方式去读写文件,然而,在Android 11上,Google又给了它新的含义,来看看官网的原话

image

也就是说,在Android 11设备上ÿ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值