简体   繁体   English

Kotlin,如果点击对话框的按钮时出现错误,如何重试请求

[英]Kotlin, how can retry request if get error when tap on button of dialog

Let imagine situation on Kotlin, when we try get request but hasn't internet connection and we get error, then show AlertDialog, and we need retry request if user click on "positive button". 让我们想象一下Kotlin上的情况,当我们尝试获取请求但没有互联网连接并收到错误消息时,然后显示AlertDialog,如果用户单击“正按钮”,则需要重试请求。

This method check for exist user by phone number: 此方法通过电话号码检查现有用户:

override fun checkPhone(phone: String, context: Context) {
    view?.let {
        it.showOrHideProgressBar(true)
        apiManager.checkPhone(phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe({ result ->
                    view?.showOrHideProgressBar(false)
                    if (result.user_exists) {
                        view?.showLogin()
                    } else {
                        val code = result.confirmation_code
                        confirmPhone(phone, code, context)
                    }
                }, { error ->
                    handleAnyError(error, context)
                }).addToCompositeDisposable(compositeDisposable)
    }
}

And here common method for handle errors: 这里是处理错误的常用方法:

private fun handleAnyError(it: Throwable, context: Context) {
    view?.showOrHideProgressBar(false)
    when (it) {
        is SocketTimeoutException -> showDialogWithException(context)
        is UnknownHostException -> showDialogWithException(context)
        else -> {
            if (it.message.equals(MESS_429)) {
                view?.showAnyError(context.getString(R.string.err_429))
            } else if (it.message.equals(MESS_422)) {
                view?.showAnyError(context.getString(R.string.err_422))
            }
        }
    }
}

Finally, method to show dialog suggesting retrying request when click positive button: 最后,单击肯定按钮时显示对话框提示重试请求的方法:

fun showDialogWithException(context: Context) {
if (!(context as Activity).isFinishing) {
    DialogInternetUtils().showOkDialog(
            context,
            context.getString(R.string.no_internet_connection),
            DialogInterface.OnClickListener
            { dialogInterface, i ->
                dialogInterface?.dismiss()
                if (i == Dialog.BUTTON_NEGATIVE) {
                    dialogInterface!!.dismiss()
                    return@OnClickListener
                } else if (i == Dialog.BUTTON_POSITIVE) {
                    // here handle click button positive, need retry request
                }
            })
}

} }

Please, help me finish handle click on positive button to retry request. 请帮我完成处理,点击肯定按钮重试请求。 I guess, need handle error in methods .doOnError() or .onErrorResumeNext() but I'm stuck here... 我猜想在方法.doOnError().onErrorResumeNext()需要处理错误,但是我被.onErrorResumeNext() ...

You could pass a function to be executed when the dialog button is clicked: 单击对话框按钮时,可以传递一个要执行的函数:

fun showDialogWithException(context: Context, action: () -> Unit) {
  if (!(context as Activity).isFinishing) {
    DialogInternetUtils().showOkDialog(
        context,
        context.getString(R.string.no_internet_connection),
        DialogInterface.OnClickListener
        { dialogInterface, i ->
          dialogInterface?.dismiss()
          if (i == Dialog.BUTTON_NEGATIVE) {
            dialogInterface!!.dismiss()
            return@OnClickListener
          } else if (i == Dialog.BUTTON_POSITIVE) {
            action()
          }
        })
  }
}
private fun handleAnyError(it: Throwable, context: Context, action: () -> Unit) {
  view?.showOrHideProgressBar(false)
  when (it) {
    is SocketTimeoutException -> showDialogWithException(context, action)
    is UnknownHostException -> showDialogWithException(context, action)
    else -> {
      if (it.message.equals(MESS_429)) {
        view?.showAnyError(context.getString(R.string.err_429))
      } else if (it.message.equals(MESS_422)) {
        view?.showAnyError(context.getString(R.string.err_422))
      }
    }
  }
}
override fun checkPhone(phone: String, context: Context) {
  view?.let {
    it.showOrHideProgressBar(true)
    apiManager.checkPhone(phone)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.io())
        .subscribe({ result ->
          view?.showOrHideProgressBar(false)
          if (result.user_exists) {
            view?.showLogin()
          } else {
            val code = result.confirmation_code
            confirmPhone(phone, code, context)
          }
        }, { error ->
          handleAnyError(error, context, { checkPhone(phone, context) })
        }).addToCompositeDisposable(compositeDisposable)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM