Это моя функция шифрования. Я должен изменить конечную точку и добавить параметры по запросу Post / Put. Get работает. Когда я снимаю .post(formBody.build())
функцию. Это ответ 200, но если я добавлю post(formBody)
. Он вернет 400. Любое решение?
private fun encryptMethod(chain: Interceptor.Chain, method: String): Response {
var params: HashMap<String, String> = HashMap()
if (chain.request().body() is FormBody){
params = getHttpUrlParams(chain.request().body() as FormBody)
}
val url: HttpUrl = chain.request().url()
val urlBuilder = url.newBuilder()
//將舊的path改成params
var pathParams = ""
for (position in 0 until url.pathSegments().size) {
if (position < url.pathSize()) pathParams += "/"
pathParams += url.pathSegments()[position]
}
//更新path
for (position in 0 until url.pathSegments().size) {
urlBuilder.removePathSegment(0)
}
urlBuilder.addPathSegment("api")
params["path"] = pathParams
params["method"] = method
params["timestamp"] = (System.currentTimeMillis() / 1000).toString()
Log.i("ddd","params $params ")
val formBody = FormBody.Builder()
//將encryptParams加入post
for ((key, value) in NetworkEncrypt.encryptParams(params)) {
formBody.add(key, value)
}
val builder = chain
.request()
.newBuilder()
.post(formBody.build())
.url(urlBuilder.build())
Log.i("ddd",builder.build().toString())
val request = builder.build()
return chain.proceed(request)
}
private fun getHttpUrlParams(formBody: FormBody?): HashMap<String, String> {
val paramMap: HashMap<String, String> = HashMap()
if (formBody != null) {
for (i in 0 until formBody.size()) {
paramMap[formBody.name(i)] = formBody.value(i)
}
}
return paramMap
}