Retrofit2 API 从不请求 returns。为什么?
Retrofit2 API request never returns. Why?
我有一个 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)
}
}
服务器 returns 浏览器中的正常 HTTP200 文本正文响应(我在这里使用虚拟 API)。但是我无法使用 retrofit2 服务获取它(该功能从不 returns):
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
}
检查是否有效,
编辑:
此外,您应该参考 docs,并检查如何使用 @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
}
我有一个 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)
}
}
服务器 returns 浏览器中的正常 HTTP200 文本正文响应(我在这里使用虚拟 API)。但是我无法使用 retrofit2 服务获取它(该功能从不 returns):
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
}
检查是否有效,
编辑:
此外,您应该参考 docs,并检查如何使用 @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
}