【问题标题】:how to solve Too many arguments for public abstract fun enqueue(responseCallback: Callback): Unit defined in okhttp3.Call如何解决公共抽象乐趣入队的参数过多(responseCallback:Callback):okhttp3.Call中定义的单位
【发布时间】:2020-12-21 01:58:30
【问题描述】:

我关注这个关于 kotlin 编程语言解析 API 的教程:https://www.youtube.com/watch?v=2W41M9fWf6I&list=PL0dzCUj1L5JGfHj1lwxOq67zAJV3e1S9S&index=5

为了关注下面的教程链接,我使用 androidx api 版本 30

但我在大约 14:54 卡住了

"client.newCall(request).enqueue(object : Callback){

        }"

我在 CourseDetailActivity 的第 (45) 行遇到错误

错误是:

Too many arguments for public abstract fun enqueue(responseCallback: Callback): Unit defined in   okhttp3.Call

这是我的 CourseDetailActivity :

class CourseDetailActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        recyclerView_main.setBackgroundColor(Color.RED)
        recyclerView_main.layoutManager = LinearLayoutManager(this)
        recyclerView_main.adapter = CourseDetailAdapter()

        val navbarTitle = intent.getStringExtra(CustumViewHolder.VIDEO_TITLE_KEY)
        supportActionBar?.title = navbarTitle


//        println(courseDetailUrl)

        fetcJson()


    }

    private fun fetcJson() {
        val videoID = intent.getIntExtra(CustumViewHolder.VIDEO_ID_KEY, -1)
        val courseDetailUrl = "https://api.letsbuildthatapp.com/youtube/course_detail?id=" + videoID

        val client = OkHttpClient()
        val request = Request.Builder().url(courseDetailUrl).build()
        client.newCall(request).enqueue(object : Callback){

        }
    }


    private class CourseDetailAdapter : RecyclerView.Adapter<CourseLessonViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CourseLessonViewHolder {


            val layoutInflater = LayoutInflater.from(parent?.context)
            val custumView = layoutInflater.inflate(R.layout.course_lesson_row, parent, false)

//            val blueView = View(parent?.context)
//            blueView.setBackgroundColor(Color.RED)
//            blueView.minimumHeight = 50
            return CourseLessonViewHolder(custumView)


        }

        override fun getItemCount(): Int {
            return 5
        }

        override fun onBindViewHolder(holder: CourseLessonViewHolder, position: Int) {

        }


    }
}

private class CourseLessonViewHolder(val custumView: View) : RecyclerView.ViewHolder
    (custumView) {

}

这是我的 MainAdapter.kt 类:

class MainAdapter(val homeFeed: HomeFeed) : RecyclerView.Adapter<CustumViewHolder>() {

    val videoTitles = listOf("First Title", "Second", "3rd")

    override fun getItemCount(): Int {
        return homeFeed.videos.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustumViewHolder {
        val layoutInflater = LayoutInflater.from(parent?.context)
        val cellForRow = layoutInflater.inflate(R.layout.video_row, parent, false)
        return CustumViewHolder(cellForRow)
    }

    override fun onBindViewHolder(holder: CustumViewHolder, position: Int) {

        val video = homeFeed.videos.get(position)
        holder?.view?.textView_video_title?.text = video.name

        holder?.view?.textview_channel_name?.text =
            video.channel.name + "+" + "20k Views\n" + "2 days ago"

        val thumbnailImageView = holder?.view?.imageView_video_thumbnail

        Picasso.get().load(video.imageUrl).into(thumbnailImageView)


        val channelProfileImageView = holder?.view?.imageView_channel_profile
        Picasso.get().load(video.imageUrl).into(channelProfileImageView)

        holder?.video = video

    }
}

class CustumViewHolder(val view: View, var video: Video? = null) : RecyclerView.ViewHolder(view) {
    companion object {
        val VIDEO_TITLE_KEY = "VIDEO_TITLE"
        val VIDEO_ID_KEY = "VIDEO_ID"
    }

    init {
        view.setOnClickListener {
            val intent = Intent(view.context, CourseDetailActivity::class.java)

            intent.putExtra(VIDEO_TITLE_KEY, video?.name)
            intent.putExtra(VIDEO_ID_KEY, video?.id)

            view.context.startActivity(intent)
        }
    }

}

【问题讨论】:

    标签: android api android-studio kotlin okhttp


    【解决方案1】:

    你在实现接口“回调”的匿名类中有一个错误。

    改为这个

    client.newCall(request).enqueue(object : Callback){
    
    }
    

    使用这个

    client.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call, e: IOException) {}
    
        override fun onResponse(call: Call, response: Response) {}
    })
    

    【讨论】:

    • 哇,谢谢你,这对我有用。我犯了将花括号放在括号外的错误。哈哈,我很傻
    猜你喜欢
    • 2020-12-14
    • 2020-10-03
    • 2019-11-23
    • 2020-04-30
    • 2018-08-25
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 2017-06-05
    相关资源
    最近更新 更多