【问题标题】:Retrofit - Enqueue - Kotlin改造 - 入队 - Kotlin
【发布时间】:2021-05-25 16:52:00
【问题描述】:
class MainActivity : AppCompatActivity() {
    private val fizzUrl = "https://s3-us-west-1.amazonaws.com/fizzup/files/public/"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    //Instance Retrofit
    val retrofit = Retrofit.Builder()
        .baseUrl(fizzUrl)
        .addConverterFactory(MoshiConverterFactory.create())
        .build()

    //Instance Api
    val service = retrofit.create(ExerciseService::class.java)

    //Get Request
    val call = service.listExercises()

    //Get execution
    call.enqueue(object: Callback<List<Exercise>> {
        override fun onResponse(call: Call<List<Exercise>>, response: Response<List<Exercise>>) {
            val allExercise = response.body()
            if (allExercise!= null) {
                println("All exercises are loaded :")
                for (c in allExercise)
                    println(" one exercise : ${c.name}")
            }
        }
        override fun onFailure(call: Call<List<Exercise>>, t: Throwable) {
            error("KO")
        }
    })
}

这是我的主要活动。我有一个数据模型:练习 我有一个接口ExerciceService。 但我对 enqueue 方法有一个问题,告诉我:期待成员声明 因此,我对对象还有另一个问题:预期名称。 但是我已经查看了如何使用 enqueue 并且我所看到的只是我所做的。 感谢您阅读我,抱歉英语不好。

【问题讨论】:

  • 请创建一个有意义的标题,描述您的问题。标签根本不应该出现在标题中,更不用说 100% 了。

标签: kotlin retrofit retrofit2


【解决方案1】:

@Xetiam,

检查是否正确导入了回调类。

您也可以在这里查看。他们有很好的文档。

https://site-valley.com/2021/02/17/retrofit-android-tutorial-in-kotlin/

【讨论】:

  • import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.example.myapplication.api.ExerciseService import com.example.myapplication.model.Exercise import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory 这是我的导入。不过谢谢,我会看看你给我的。
  • @Xetiam 有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2018-02-15
  • 2014-10-28
  • 2016-08-01
  • 2018-08-31
相关资源
最近更新 更多