OkHttpClient() POST 无法正常工作的问题 KOTLIN

发布于 2025-01-13 23:53:02 字数 2080 浏览 1 评论 0原文

我正在尝试进行同步调用,该调用需要在继续将用户存储在云中之前完成。我相信问题出在 RequestBody 中,因为它看起来只是一个字节数组。下面是代码:

                            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()
                                   }
                               }


代码不会崩溃,但调用似乎永远不会完成,因为它永远不会进入 it.isSuccessful 或 !it.isSuccessful。也许它是一个错误的形成调用。如果可以的话请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2025-01-20 23:53:02

尝试使用回调对请求进行排队并管理响应:

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()
    }
}

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()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文