Android 改型2 API请求永远不会返回。为什么?
我有一个API服务:Android 改型2 API请求永远不会返回。为什么?,android,kotlin,retrofit2,Android,Kotlin,Retrofit2,我有一个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 {
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)
}
}
为什么?不要认为你应该将
suspend
与Call
一起使用,因为它们都在延迟
试着换成
接口服务{
@得到
suspend fun getInt():Int
}
检查它是否有效
编辑:
另外,您应该参考并检查如何使用@Path
和@Query
private const val BASE\u URL=”https://www.random.org/"
接口服务{
@获取(“整数/”)
暂停娱乐(
@查询(“num”)num:Int,
@查询(“min”)min:Int,
@查询(“max”)max:Int,
@查询(“列”)列:Int,
@查询(“基”)基:Int,
@查询(“格式”)格式:字符串,
@查询(“rnd”)rnd:String,
):Int
}
reformation.create(FooApiService::class.java)如何初始化改型?谢谢,这通常是正确的(我承认人为的基本URL是错误的;您不能有查询参数,否则会导致异常)。另外,async不会重新抛出异常,这就是为什么所有的东西都会保持沉默。删除呼叫修复了我的代码。
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?")
}
}