【发布时间】:2021-12-22 14:25:36
【问题描述】:
我已经构建了一个拦截器,并且我有一个 API 调用响应,它一遍又一遍地返回一系列表情符号,大小超过 25MB,因此在进行此特定网络调用时应用程序崩溃。我想完全排除响应中的表情符号,或者只是将大小缩小到一个表情符号。
我已经编写了这个拦截器并查看了文档:https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/as-response-body/
如何在 Kotlin 中将此值作为响应正文返回?对改造拦截器的任何帮助将不胜感激。
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
import okio.IOException
abstract class Interceptor() : Interceptor {
/** This interceptor compresses the HTTP request body. */
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val originalRequest: Request = chain.request()
val response: okhttp3.Response = chain.proceed(originalRequest)
if (response.body != null) {
val byteResponse = response.body?.contentLength()!!
// return response.body?.contentLength:Long= -1L):ResponseBody
if (byteResponse > 500000) {
val shorter =( byteResponse -1L)
shorter.asResponseBody(). // this line is the problem as it can't just be cast to a response body return
}
}
return response
}
}```
【问题讨论】:
标签: android kotlin retrofit okhttp interceptor