改进拦截器- Android,如果API响应正文非常大,则使用Kotlin和OKHTTP
我已经构建了一个拦截器,并且我有一个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
}
}```
转载请注明出处:http://www.intsu.net/article/20230509/1011811.html