最新的retrofit + kotlin

305 阅读1分钟

retrofit + kotlin

最新的retrofit(2.6.0以上)可以直接使用suspend来标示方法,直接返回你需要的对象。

interface Api {
    @GET("/query")
    suspend fun appList(@Query("app") app: String, @Query("type") type: Int): AppListData
}

比如这样。

使用起来也很简单。

        viewModelScope.launch {
                val res = apiService.appList(appName, type)
            }
        }

其中apiService可以这样定义:

val apiService: Api by lazy {
    MyRetrofit.getRetrofit().create(Api::class.java)
}