当输入错误的详细信息时,改造响应将变为 Null
Retrofit response is getting Null when typed wrong details
在登录表单中 --> 当输入正确的用户详细信息(如电子邮件和密码)时,它会成功将我带到下一个 activity 但是当输入错误的详细信息时,Retrofit 响应将变为 Null
在 postman 中 --> 错误信息显示正确
但不在应用吐司中,它显示为 null
这是我的 activity-->
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)
}
在登录表单中 --> 当输入正确的用户详细信息(如电子邮件和密码)时,它会成功将我带到下一个 activity 但是当输入错误的详细信息时,Retrofit 响应将变为 Null
在 postman 中 --> 错误信息显示正确 但不在应用吐司中,它显示为 null
这是我的 activity-->
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)
}