如何解决公共抽象乐趣入队的参数过多(responseCallback:Callback):okhttp3.Call中定义的单位

Posted

技术标签:

【中文标题】如何解决公共抽象乐趣入队的参数过多(responseCallback:Callback):okhttp3.Call中定义的单位【英文标题】:how to solve Too many arguments for public abstract fun enqueue(responseCallback: Callback): Unit defined in 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)
        
    


【问题讨论】:

【参考方案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) 
)

【讨论】:

哇,谢谢你,这对我有用。我犯了将花括号放在括号外的错误。哈哈,我很傻

以上是关于如何解决公共抽象乐趣入队的参数过多(responseCallback:Callback):okhttp3.Call中定义的单位的主要内容,如果未能解决你的问题,请参考以下文章

Variadic Macros:如何解决“宏的实际参数过多..”

对于公共乐趣 Fragment.findNavController() 的许多论点

“T”必须是具有公共的无参数构造函数的非抽象类型,才能用作泛型类型或方法

如何防止具有公共派生类的抽象类在其他程序集中被继承?

java 判断条件过多怎么办?有啥好的解决办法吗?

初识Django