【发布时间】:2020-06-19 05:30:37
【问题描述】:
在登录时,实际上我的成功响应在 toast 上正确显示。但是当输入错误的详细信息时,错误正文响应没有正确显示
这是我的回应-->
{
"status": 401,
"data": false,
"message": "User login unsuccessful.",
"user_msg": "Email or password is wrong. try again"
}
我的以下代码:
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()
Log.d("kjsfgxhufb",response.body()?.status.toString())
startActivity(intent)
finish()
}
else
{
try {
val jObjError =
JSONObject(response.errorBody()!!.string())
Toast.makeText(
applicationContext,
jObjError.getJSONObject("user_msg").getString("message"),
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
}
}
我的输出是吐司:
Value Email or password is wrong. try again at user_msg of type java.lang.String cannot be
converted to JSONObject
实际上我不想吐司这样的错误消息..我想要简单的消息,例如“电子邮件或密码错误。再试一次”
帮帮我,谢谢
【问题讨论】:
标签: android json kotlin retrofit retrofit2