我有一个API服务。
private const val BASE_URL = "https://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new"
interface FooApiService {
@GET
suspend fun getInt():
Call<Int>
}
object FooApi {
val retrofitService : FooApiService by lazy {
retrofit.create(FooApiService::class.java)
}
}
服务器在浏览器中返回一个正常的HTTP200文本体响应(我在这里使用的是一个虚拟API)。然而,我无法使用 Retrofit2 服务获得它(该函数从未返回)。
private var job = Job()
private val fooScope = CoroutineScope(job + Dispatchers.IO)
private fun doStuff() {
fooScope.async {
FooApi.retrofitService.getInt().execute()
Log.i(TAG, "We never reach here! Why?")
}
}
为什么?
不认为你应该使用 suspend
与 Call
因为他们俩都在推迟改成的尝试
interface FooApiService {
@GET
suspend fun getInt(): Int
}
检查一下是否有效。
编辑:另外,你应该参考 文件并检查如何使用 @Path
和 @Query
private const val BASE_URL = "https://www.random.org/"
interface FooApiService {
@GET("integers/")
suspend fun getInt(
@Query("num") num: Int,
@Query("min") min: Int,
@Query("max") max: Int,
@Query("col") col: Int,
@Query("base") base: Int,
@Query("format") format: String,
@Query("rnd") rnd: String,
): Int
}