Retrofit2 API请求永远不会返回。为什么?

问题描述 投票:-1回答:1

我有一个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?")
  }
}

为什么?

android kotlin retrofit2
1个回答
1
投票

不认为你应该使用 suspendCall因为他们俩都在推迟改成的尝试

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
}
© www.soinside.com 2019 - 2024. All rights reserved.