Home > Net >  Troubles with OkHttpClient() POST not working KOTLIN
Troubles with OkHttpClient() POST not working KOTLIN

Time:03-15

I am attempting to make a sync call that needs to complete before proceeding with storing a user in the cloud. I believe the issue is within the RequestBody as it looks like it is just a byte array. Below is the code:

                            val client = OkHttpClient()
                            val mediaType: MediaType? = "application/json".toMediaTypeOrNull()
                            val body: RequestBody =
                                RequestBody.create(mediaType, "{\"type\":\"DEFAULT\",\"name\":\"lkjlkj\"}")
                            val request: Request = okhttp3.Request.Builder()
                                .url("https://api.example.com/endpoint")
                                .post(body)
                                .addHeader("Accept", "application/json")
                                .addHeader("Content-Type", "application/json")
                                .addHeader(
                                    "Authorization",
                                    "Bearer SK-xxxxxx-4QAXH"
                                )
                                .build()


                               Toast.makeText(this@RegisterActivity, "Entering Call",Toast.LENGTH_SHORT).show()

                               val response: Unit = client.newCall(request).execute().use {
                                   Toast.makeText(this@RegisterActivity, "sent call, awaiting response",Toast.LENGTH_SHORT).show()
                                   if (it.isSuccessful){
                                       val content = JSONObject(it.body.toString())
                                       desiredString = content.getJSONArray("desiredStringField").toString()
                                       Toast.makeText(this@RegisterActivity, desiredString,Toast.LENGTH_SHORT).show()
                                   }
                                   if (!it.isSuccessful){
                                       Toast.makeText(this@RegisterActivity, "failed",Toast.LENGTH_SHORT).show()
                                   }
                               }


The code doesn't crash but it seems the call never completes, as it never makes it into the it.isSuccessful or !it.isSuccessful. Perhaps its a bad formed call somehow. Please help if you can.

CodePudding user response:

Try to enqueue the request and manage the response using a Callback:

client.newCall(request).enqueue(object : Callback {
    override fun onResponse(call: Call, response: Response) {
        if (!response.isSuccessful){
            Toast.makeText(this@RegisterActivity, "failed",Toast.LENGTH_SHORT).show()
            return 
        }

        try {
            val content = JSONObject(response.body?.string() ?: "")
            desiredString = content.getJSONArray("desiredStringField").toString()
            Toast.makeText(this@RegisterActivity, desiredString,Toast.LENGTH_SHORT).show()
        } catch (e: JSONException) {
            // Error parsing JSON object
        }
    }

    override fun onFailure(call: Call, e: IOException) {
        Toast.makeText(this@RegisterActivity, "failed",Toast.LENGTH_SHORT).show()
    }
}
  • Related