gpt4 book ai didi

android - 从服务器API错误输出自定义Rx Java中的onError()消息

转载 作者:行者123 更新时间:2023-12-02 12:55:15 25 4
gpt4 key购买 nike

我想打印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()
}
})
}

这是我的API在错误响应上显示的内容
    "message": "Email Already Exists",
"code": 400,
"errors": {
"error_id": "15",
"error_text": "Email Already Exists"
},
"data": []
}
我是Kotlin的新手,在这里,如有任何错误,请原谅我

最佳答案

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

关于android - 从服务器API错误输出自定义Rx Java中的onError()消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63975773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com