如何解决从服务器API错误输出自定义Rx Java中的onError消息
我想打印API消息发送的错误,例如其中有很多示例:电子邮件已经存在,用户名已经退出等。 但这仅打印HTTP 400错误请求
fun doSignUp(
firstName: String,lastName: String,email: String,password: String,deviceid: String
) {
ApiHelper().userSignup(
firstName,lastName,email,password,"Android",sTradd)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<UserResponse>{
override fun onComplete() {
Log.d("kb","onComplete Call")
}
override fun onSubscribe(d: disposable) {
Log.d("kb","onSubscribe")
}
override fun onNext(t: UserResponse) {
Toast.makeText(applicationContext,"Please Upload Profile Pic...",Toast.LENGTH_SHORT).show()
val intent = Intent(applicationContext,SignupStep3Activity::class.java)
mPref.setAccesstoken(t.data.accesstoken)
startActivity(intent)
}
override fun onError(e: Throwable) {
Toast.makeText(applicationContext,e.localizedMessage,Toast.LENGTH_LONG).show()
}
})
}
"message": "Email Already Exists","code": 400,"errors": {
"error_id": "15","error_text": "Email Already Exists"
},"data": []
}
解决方法
在您的onNext()
中,您需要这样的内容:
when (UserResponse.code()) {
200 -> {//Send appropriate message with intent here}
400 -> {//Send appropriate message with intent here}
401 -> {//Send appropriate message with intent here}
712 -> {//Send appropriate message with intent here}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。