【问题标题】:Retrofit & Interceptor - Android, using Kotlin with OKHTTP if the API response body size is very largeRetrofit & Interceptor - Android,如果 API 响应体大小非常大,则使用带有 OKHTTP 的 Kotlin
【发布时间】: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


    【解决方案1】:

    GSON 可以在这里提供帮助吗?如果您希望以更简单、更简洁的方式阅读回复。

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

    【讨论】:

      猜你喜欢
      • 2020-01-20
      • 1970-01-01
      • 2020-12-21
      • 2021-12-25
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多