提问者:小点点

改造


我已经建立了一个拦截器,我有一个API的调用响应,它一遍又一遍地返回一系列表情符号,大小超过25MB,所以当进行这个特定的网络调用时,应用程序会崩溃。我想完全排除响应中的表情符号,或者只是将大小减少到一个表情符号。

我写了这个拦截器并查看了留档:https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/as-response-body/

如何将此值作为静态编程语言中的响应体返回?对改造拦截器的任何帮助都将不胜感激。

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
  }
}```



共1个答案

匿名用户

GSON能在这里提供帮助吗?如果您希望以更简单、更清晰的方式阅读响应。

示例:https://medium.com/@pypriyank/using-rest-api-in-android-using-everfit and-gson-20268aadf0eb