普通的GET请求,参数直接拼在后面。参数中带有中文和空格需要处理
- 最开始:用URLEncoder.encode直接处理带有中文的参数
val finishStr = URLEncoder.encode(bean?.finishTime, "utf-8")
val taskName = URLEncoder.encode(bean?.taskName, "utf-8")
var shareUrl = "${UrlConfig.HOST_URL}/dist/index.html#/homeWorkShare/${APP.getUserBean().userId}" +
"/${bean?.newscore}/${bean?.newMinTime}/${bean?.highScore}/${bean?.topTen}/${bean?.onTime}" +
"/$finishStr/$taskName/6"
出现的问题:当finishStr、taskName时空格是会被转义成“+”,而不是%20,可能后台或H5的无法解析
- 增加replace("\+", "%20")替换“+”
val finishStr = URLEncoder.encode(bean?.finishTime, "utf-8").replace("\\+", "%20")
val taskName = URLEncoder.encode(bean?.taskName, "utf-8").replace("\\+", "%20")
var shareUrl = "${UrlConfig.HOST_URL}/dist/index.html#/homeWorkShare/${APP.getUserBean().userId}" +
"/${bean?.newscore}/${bean?.newMinTime}/${bean?.highScore}/${bean?.topTen}/${bean?.onTime}" +
"/$finishStr/$taskName/6"
然而结果“+”并未成功替换
- 最终:使用android.net包下的Uri.encode完美解决。中文空格问题
val finishStr = Uri.encode(bean?.finishTime, "utf-8")
val taskName = Uri.encode(bean?.taskName, "utf-8")
var shareUrl = "${UrlConfig.HOST_URL}/dist/index.html#/homeWorkShare/${APP.getUserBean().userId}" +
"/${bean?.newscore}/${bean?.newMinTime}/${bean?.highScore}/${bean?.topTen}/${bean?.onTime}" +
"/$finishStr/$taskName/6"