我不知道如何在Kotlin(Ktor框架)中设置代理服务器

时间:2019-07-19 21:19:33

标签: kotlin ktor

我有一个可以成功点击google geolocation api的函数,并且可以将JSON响应打印到终端。但是,我无法在所需的路线中正确返回它。

我尝试在路由中调用函数,返回response.body然后在路由中调用它。

    routing {
        get("/") {
            call.respondText("You should absolutely hire Robert!", contentType = ContentType.Text.Plain)
        }

        get("/geosearch") {
            call.respond(fetchGoogle())
            //'https//maps.googleapis.com/maps/api/place/autocomplete/json?input=${req.body.search}&types=geocode&key=MYKEY'
        }
    }

fun fetchGoogle(){
    val url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=5136&types=geocode&key=MYKEY"

    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)
//            return body
//            val headers: okhttp3.Headers = response.headers
//            println(headers)
        }

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

No errors, but I cannot get the response.body into the /geosearch route.

VERY basic question, but do I need to create a class in order to access the "body" of the API call?  

0 个答案:

没有答案