如何在改造错误处理程序上检索从服务器发送的errorBody?

时间:2019-04-24 16:39:00

标签: android kotlin retrofit2

一切正常后,我可以毫无问题地从response.body()取回尸体。

可以想到的是,当服务器上发生验证错误(在我的情况下为重复的电子邮件)时,它将错误发送到frontend,并且当我尝试访问错误正文时 response.errorBody()检索以下信息:

"E/API Activity: onResponse: PostSalao okhttp3.ResponseBody$1@84d856e"

我希望收到类似“错误:电子邮件已使用”之类的信息。

您可能会在(10:08)的video上看到,他所做的与我在代码中所做的相同,但是他正确地检索了错误(10:17)。

我在做什么错了?

fun postCliente(cliente: Cliente) {
        val TAG = "API Activity"
        val apiInterface: ApiInterface
        apiInterface = ClientApi.getClient().create(ApiInterface::class.java)
        val clientePostCall = apiInterface.postCliente(cliente)
        mProgressBar.visibility = View.VISIBLE

        clientePostCall.enqueue(object: Callback<Cliente> {
            override fun onResponse(call: Call<Cliente>, response: Response<Cliente>) {
                mProgressBar.visibility = View.GONE
                if(response.isSuccessful){
                    try {
                    Toast.makeText(context,"Usuario " + response.body()!!.name + " criado com sucesso.",Toast.LENGTH_SHORT).show()
                        val backHomeIntent = Intent(context, MainActivity::class.java)
                        context.startActivity(backHomeIntent)
                    } catch (e: NullPointerException) {
                        Toast.makeText(context, "Problem is unknown: ", Toast.LENGTH_SHORT).show()
                    }
                }else {
                    try {
                        val errorBody = response.errorBody().toString()


                        Toast.makeText(context, "Dados incorretos", Toast.LENGTH_SHORT).show()
                            Log.e(TAG, "onResponse: " + errorBody )

                    } catch (e: IOException){
                        Toast.makeText(context, "Problem is unknown: ", Toast.LENGTH_SHORT).show()
                    }
                }
            }
            override fun onFailure(call: Call<Cliente>, t: Throwable) {
                mProgressBar.visibility = View.GONE
                Log.e(TAG, "onFailure: " + t.localizedMessage)
            }
        })
    }

Logcat on Android in red

1 个答案:

答案 0 :(得分:1)

您必须使用string()而不是toString()来获取正确的错误消息。因此您的代码应如下所示:

fun postCliente(cliente: Cliente) {
    val TAG = "API Activity"
    val apiInterface: ApiInterface
    apiInterface = ClientApi.getClient().create(ApiInterface::class.java)
    val clientePostCall = apiInterface.postCliente(cliente)
    mProgressBar.visibility = View.VISIBLE

    clientePostCall.enqueue(object: Callback<Cliente> {
        override fun onResponse(call: Call<Cliente>, response: Response<Cliente>) {
            mProgressBar.visibility = View.GONE
            if(response.isSuccessful){
                try {
                Toast.makeText(context,"Usuario " + response.body()!!.name + " criado com sucesso.",Toast.LENGTH_SHORT).show()
                    val backHomeIntent = Intent(context, MainActivity::class.java)
                    context.startActivity(backHomeIntent)
                } catch (e: NullPointerException) {
                    Toast.makeText(context, "Problem is unknown: ", Toast.LENGTH_SHORT).show()
                }
            }else {
                try {
                    val errorBody = response.errorBody().string()


                    Toast.makeText(context, "Dados incorretos", Toast.LENGTH_SHORT).show()
                        Log.e(TAG, "onResponse: " + errorBody )

                } catch (e: IOException){
                    Toast.makeText(context, "Problem is unknown: ", Toast.LENGTH_SHORT).show()
                }
            }
        }
        override fun onFailure(call: Call<Cliente>, t: Throwable) {
            mProgressBar.visibility = View.GONE
            Log.e(TAG, "onFailure: " + t.localizedMessage)
        }
    })
}