这将添加一个参数,或替换 Kotlin 中现有参数的值。
Uri 的扩展:
fun Uri.addUriParameter(key: String, newValue: String): Uri {
val params = queryParameterNames
val newUri = buildUpon().clearQuery()
var isSameParamPresent = false
for (param in params) {
// if same param is present override it, otherwise add the old param back
newUri.appendQueryParameter(param,
if (param == key) newValue else getQueryParameter(param))
if (param == key) {
// make sure we do not add new param again if already overridden
isSameParamPresent = true
}
}
if (!isSameParamPresent) {
// never overrode same param so add new passed value now
newUri.appendQueryParameter(key,
newValue)
}
return newUri.build()
}
执行:
val appendedURL = originalUri.addUriParameter("UID","123456")