Search code examples
androidkotlinokhttp

Getting an error Using 'receivedResponseAtMillis(): Long' is an error. moved to val in okhttp


I am getting this error after I update okhttp 4.12.0. Here is my below code where I am getting the issue.

fun retrofit2.Response<*>.responseTimeMs(): Long =
    raw().run {
      receivedResponseAtMillis() - sentRequestAtMillis()
}

I have also moved that to val and tried like below. But it is not working.

fun retrofit2.Response<*>.responseTimeMs(): Long {
  val received = raw().receivedResponseAtMillis()
  val sent = raw().sentRequestAtMillis()
  return (received - sent)
}

Please let me know how to resolve this issue.


Solution

  • Using 'receivedResponseAtMillis(): Long' is an error. moved to val

    This message is indicating that you have to use val property receivedResponseAtMillis instead of function call receivedResponseAtMillis(). It is not about creating a reference val received = raw().receivedResponseAtMillis() as in your code snippet.

    IDEs generally show a quick fix for that error such as Replace with 'receivedResponseAtMillis'

    This extension function should work.

    fun retrofit2.Response<*>.responseTimeMs(): Long =
        raw().run {
            receivedResponseAtMillis - sentRequestAtMillis
        }
    

    Here is the deprecated annotation for receivedResponseAtMillis()

    // OkHttp v4.12.0
    
    @JvmName("-deprecated_receivedResponseAtMillis")
      @Deprecated(
          message = "moved to val",
          replaceWith = ReplaceWith(expression = "receivedResponseAtMillis"),
          level = DeprecationLevel.ERROR)
      fun receivedResponseAtMillis(): Long = receivedResponseAtMillis