给OkHttp添加公共请求参数的Interceptor
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
implementation 'com.github.simplepeng:ParamsInterceptor:v1.0.2'
private val params = ConcurrentHashMap<String, String>().apply {
"params1" to "simple"
"params2" to "peng"
"params3" to "hahaha"
}
val httpClient = OkHttpClient.Builder()
.addInterceptor(ParamsInterceptor(params, inPath = true, onPreRequest = { params ->
params["params3"] = "hei hei hei"
params["params4"] = "tu tu tu"
}))
class ParamsInterceptor
@JvmOverloads constructor(
//公共参数们
private val params: ConcurrentHashMap<String, String>,
//过滤掉不添加公共参数的url
private val excludeUrls: List<String> = listOf(),
//公共参数是添加到url上还是请求体中
private val inPath: Boolean = false,
//请求执行前的回调
private val onPreRequest: (params: MutableMap<String, String>) -> Unit = {}
)
-
v1.0.2:增加其他请求方式的适配
-
v1.0.1:解决线程并发执行发生
ConcurrentModificationException
的bug -
v1.0.0:首次上传