'How do we catch the response of HttpsPost android

I am trying to catch the response of Facebook Graph API with the following function

override fun onClick(v: View?) {
    val url = URL("https://graph.facebook.com/v13.0/device/login");
    val client: HttpsURLConnection
    try {
        client = url.openConnection() as HttpsURLConnection
        client.requestMethod = "POST"
        client.setRequestProperty(
            "access_token", 
            resources.getString(R.string.facebook_app_id) + "|" + R.string.facebook_client_token
        )
        client.setRequestProperty("scope", "public_profile")
        client.doOutput = true
        Thread {
            val outputPost = BufferedOutputStream(client.outputStream)
            Handler(Looper.getMainLooper()).post {
                val byte = byteArrayOf(127)
                outputPost.write(byte)
                Log.e("MAL", byte.toString())
                outputPost.flush();
                outputPost.close()
            }
        }.start()
    } catch (error: MalformedURLException) {
        error.message?.let { Log.e("MA", it) }
    } catch (error: SocketTimeoutException) {
        error.message?.let { Log.e("MA", it) }
    } catch (error: IOException) {
        error.message?.let { Log.e("MA", it) }
    }
}

I want to send the response to Log.e or save it to a string. The response is a json. I will write the log in Handle(Looper... method



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source