密码和确认密码输入错误时,错误响应500不会被播放 [英] Error Response 500 is not getting diaplayed when password and confirm password typed wrong

查看:86
本文介绍了密码和确认密码输入错误时,错误响应500不会被播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当密码和确认密码正确输入时,我的回复成功显示....但是当密码不匹配时,它不会传给其他部分....不显示吐司面包......请指导我...需要帮助...提前感谢

my response getting successfully displayed when password and confirm password typed right....but when it mismatched it is not going to else part....not showing toast anything......please guide me ...need help...thanks in advance

这是我在邮递员中输入密码并确认密码错误时的答复:-

here is my response when password and confirm password typed wrong in postman:-

{
"status": 500,
"message": "Could not reset password.",
"error": {
    "confirm_password": {
        "compareWith": "Passwords mismatch."
    }
},
"user_msg": "Could not reset password, please try again."
}

这是我的数据类:-

1->

data class Reset_Reponse_Base (
@SerializedName("status") val status : Int,
@SerializedName("message") val message : String,
@SerializedName("error") val error : Reset_Response_error,
@SerializedName("user_msg") val user_msg : String
)

2->

 data class Reset_Response_error (
@SerializedName("confirm_password") val confirm_password : Reset_Response_Confirm_password

 )

3->

data class Reset_Response_Confirm_password (
@SerializedName("compareWith") val compareWith : String
 )

我的回复呼叫活动:--->

 var old_password = findViewById<TextView>(R.id.old_password)
    var new_password = findViewById<TextView>(R.id.new_password)
    var confirm_password =
        findViewById<TextView>(R.id.confirm_new_password)
    val resetpwdbutton = findViewById<Button>(R.id.resetpwdbtn)
    resetpwdbutton.setOnClickListener {
        val old = old_password.text.toString().trim()
        val new = new_password.text.toString().trim()
        val confirm = confirm_password.text.toString().trim()
       
        val token: String =
            SharedPrefManager.getInstance(
                applicationContext
            ).user.access_token.toString()
        RetrofitClient.instance.resetpassword(token, old, new, confirm)
            .enqueue(object : Callback<Reset_Reponse_Base> {
                override fun onFailure(call: Call<Reset_Reponse_Base>, t: Throwable) {

                    Log.d("res", "" + t)


                }

                override fun onResponse(
                    call: Call<Reset_Reponse_Base>,
                    response: Response<Reset_Reponse_Base>
                ) {
                    var res = response

                    if (res.body()?.status == 200) {

                        Log.d("response check ", "" + response.body()?.status.toString())
                        if (res.body()?.status == 200) {
                            Toast.makeText(
                                    applicationContext,
                            res.body()?.user_msg,
                            Toast.LENGTH_LONG
                            ).show()
                            Log.d("kjsfgxhufb", response.body()?.user_msg.toString())
                         

                        } else  if (res.body()?.status == 500){
                            val ret: Reset_Response_error = res.body()!!.error
                            val ret2: Reset_Response_Confirm_password = ret.confirm_password
                            //confused over here-->
                            //toast is not getting diaplayed when password and confirm password doesnt match
                            try {
                                val jObjError =
                                    JSONObject(response.errorBody()!!.string())
                                Toast.makeText(
                                    applicationContext,
                                    jObjError.getString("message")+jObjError.getString("user_msg"),
                                    Toast.LENGTH_LONG
                                ).show()
                            } catch (e: Exception) {
                                Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
                                Log.e("errorrr",e.message)
                            }
                        }

                    }
                }
            })
    }

else if (!res.isSuccessful ==>替换此else if (res.body()?.status == 500)后,在调试器中为假

After replacing this else if (res.body()?.status == 500) by else if (!res.isSuccessful ==> false in debugger

但如果我这样做-> else if (res.errorBody()?.equals(500)!!)

but if i do this-->else if (res.errorBody()?.equals(500)!!)

我正在得到这个->

im getting this -->

我要检索==>消息":无法重设密码."错误":{"co ......

i want to retrive ==> message":"Could not reset password."error":{"co…...

推荐答案

您必须解析错误正文.以下代码将帮助您解析错误.

You have to parse error body. Following code will help you to parse error.

您可以创建自定义错误类,以便可以针对特定的错误类型执行操作.两种异常类可以如下:

You can create your custom error class, so that you can do things for specific error type. Two exception class can be as following:

open class APIError: Exception() {
    private val statusCode = 0
    fun status(): Int {
        return statusCode
    }

    fun message(): String? {
        return message
    }
}

class UnknownError : APIError() {}

错误解析器类可以如下.我在这里使用了kotling扩展功能来轻松使用它.

Error parser class can be as following. I've used here kotling extension function to use it easily.

fun Response<*>.parseError(): Throwable {
    val converter: Converter<ResponseBody, APIError?> = RetrofitClient.instance
            .responseBodyConverter(APIError::class.java, arrayOfNulls<Annotation>(0))
    val error: APIError?
    error = try {
        if(errorBody() != null) {
            converter.convert(errorBody())
        } else {
            return UnknownError("Something wrong")
        }
    } catch (e: IOException) {
        return APIError()
    }
    return Throwable(error)
}

最后在改造onResponse()方法中,您可以执行以下操作:

And finally in retrofit onResponse() method, you can do this:

RetrofitClient.instance.resetpassword(token, old, new, confirm)
            .enqueue(object : Callback<Reset_Reponse_Base> {
                override fun onFailure(call: Call<Reset_Reponse_Base>, t: Throwable) {

                    Log.d("res", "" + t)


                }

                override fun onResponse(
                        call: Call<Reset_Reponse_Base>,
                        response: Response<Reset_Reponse_Base>
                ) {
                    var res = response
                    if(response.isSuccessful) {
                        if (res.body().status == 200) {

                            Log.d("response check ", "" + response.body()?.status.toString())
                            if (res.body()?.status == 200) {
                                Toast.makeText(
                                        applicationContext,
                                        res.body()?.user_msg,
                                        Toast.LENGTH_LONG
                                ).show()
                                Log.d("kjsfgxhufb", response.body()?.user_msg.toString())


                            } else if (res.body()?.status == 500) {
                                val ret: Reset_Response_error = res.body()!!.error
                                val ret2: Reset_Response_Confirm_password = ret.confirm_password
                                //confused over here-->
                                //toast is not getting diaplayed when password and confirm password doesnt match
                                try {
                                    val jObjError =
                                            JSONObject(response.errorBody()!!.string())
                                    Toast.makeText(
                                            applicationContext,
                                            jObjError.getString("message") + jObjError.getString("user_msg"),
                                            Toast.LENGTH_LONG
                                    ).show()
                                } catch (e: Exception) {
                                    Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
                                    Log.e("errorrr", e.message)
                                }
                            }

                        }
                    } else {
                        val error = res.parseError()
                        // do your stuffs with error
                    }
                }
            })

这篇关于密码和确认密码输入错误时,错误响应500不会被播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆