Home > Net >  Error: Unresolved Reference: url (Okhttp3, Kotlin, android studio)
Error: Unresolved Reference: url (Okhttp3, Kotlin, android studio)

Time:11-03

I'm trying to fetch JSON data using okhttp3, but I'm getting and error in this code as e: Unresolved reference: url.

private fun fetchJson() {
    println("Attempting to fetch JSON")

    val url = "https://lordvarun.github.io/back/data.json"

    val request = Request.Builder.url(url).build()

    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback {
        override fun onResponse(call: Call, response: Response) {
            val body = response.body?.string()
            println(body)

            val gson = GsonBuilder().create()

            val homeFeed = gson.fromJson<>(body, HomeFeed::class.java)
        }

        override fun onFailure(call: Call, e: IOException) {
            println("Failed to execute request")
        }
    })
}

I rechecked all the dependeicies and imports. Why is url of all things an unresolved reference?

CodePudding user response:

You forgot the parenthesis in the Builder constructor call

val request = Request.Builder().url(url).build()

CodePudding user response:

There are minor mistake in typing

val request = Request.Builder.url(url).build()

simply change to this
val request = Request.Builder().url(url).build()

  • Related