无法在Kotlin中使用OKHttp3获得API请求响应

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

我正在尝试使用kotlin中的OKHttp库对API进行GET请求调用。

这是我的代码的样子:

    val client = OkHttpClient()
    val url ="https://api.letsbuildthatapp.com/youtube/home_feed"   //valid example
    val request = Request.Builder().url(url).build()

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

       override fun onResponse(call: Call, response: Response) {
            val body = response?.body?.string()
            println(body)
        }

        override fun onFailure(call: Call, e: IOException) {
            println("failed request execution")
        }

})

问题是每当执行调用时,显示显示自定义消息“失败的请求执行”onFailure方法。

允许Internet权限,配置了网络安全,Logcat是未显示任何类型的错误。

我真的很坚持,可能是什么问题?

android api kotlin okhttp
1个回答
0
投票

您未声明HTTP Verb,端点通过GET Http Verb提供数据。因此,您必须专门定义HTTP Verb,像这样

Request.Builder().url(url).get().build()
© www.soinside.com 2019 - 2024. All rights reserved.