当键入错误的详细信息时,改装响应为Null



在登录表单-->中,当键入正确的用户详细信息(如电子邮件和密码(时,它会成功地将我带到下一个活动,但当键入错误的详细信息时,改装响应会得到Null

在邮递员-->中,错误消息显示正确但不是在应用程序toast中,它显示空

这是我的活动-->

RetrofitClient.instance.userLogin(email, password)
.enqueue(object : Callback<LoginResponse> {
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
Log.d("res", "" + t)
}
override fun onResponse(
call: Call<LoginResponse>,
response: Response<LoginResponse>
) {
var res = response
Log.d("response check ", "" + response.body()?.status.toString())
if (res.body()?.status==200) {
SharedPrefManager.getInstance(applicationContext)
.saveUser(response.body()?.data!!)
val intent = Intent(applicationContext, HomeActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
Toast.makeText(
applicationContext,
res.body()?.message,
Toast.LENGTH_LONG
).show()
startActivity(intent)
finish()
} else {
//Log.d("res", "" +  res.body()?.status.toString())
Toast.makeText(
applicationContext, (res.body()?.status.toString())
,
Toast.LENGTH_LONG
).show()
}

提前感谢将不胜感激

使用这个,

res.code()

而不是

res.body()?.status

我终于得到了答案:

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

最新更新