Coder Social home page Coder Social logo

Comments (1)

VIPyinzhiwei avatar VIPyinzhiwei commented on August 24, 2024

Retrofit的请求返回Call有点累赘,这样还要自己扩张.await方法。

@GET
fun getDaily(@Url url: String): Call<Daily>

直接返回Daily不香吗?

@GET
suspend fun getDaily(@Url url: String): Daily

Call + .await方法Retrofit已经有自己的实现了,可以直接拿来用

suspend fun <T : Any> Call<T>.await(): T {
  return suspendCancellableCoroutine { continuation ->
    continuation.invokeOnCancellation {
      cancel()
    }
    enqueue(object : Callback<T> {
      override fun onResponse(call: Call<T>, response: Response<T>) {
        if (response.isSuccessful) {
          val body = response.body()
          if (body == null) {
            val invocation = call.request().tag(Invocation::class.java)!!
            val method = invocation.method()
            val e = KotlinNullPointerException("Response from " +
                method.declaringClass.name +
                '.' +
                method.name +
                " was null but response body type was declared as non-null")
            continuation.resumeWithException(e)
          } else {
            continuation.resume(body)
          }
        } else {
          continuation.resumeWithException(HttpException(response))
        }
      }

      override fun onFailure(call: Call<T>, t: Throwable) {
        continuation.resumeWithException(t)
      }
    })
  }
}

我们要做的只是正常suspend block与try catch的异常处理

收到,感谢老铁参与对Kotlin Coroutine的探讨〜

关于以上提出的两点,在Retrofit中对Coroutines的支持,其实是在Retrofit库升级> = 2.6.0开始支持的,它内置提供了对Kotlin Coroutines的支持。目前项目中Retrofit库引用的是最新的版本(2.6.1),和自己手写了await函数,其目的是为了更好的帮助新手学习理解Kotlin +协程+ Retrofit搭配使用等。

感谢理解与支持。

from eyepetizer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.