0

让我们想象一下 Kotlin 的情况,当我们尝试获取请求但没有互联网连接并且出现错误时,然后显示 AlertDialog,如果用户单击“肯定按钮”,我们需要重试请求。

此方法通过电话号码检查是否存在用户:

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

这里是处理错误的常用方法:

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

最后,单击肯定按钮时显示提示重试请求的对话框的方法:

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

}

请帮我完成处理点击肯定按钮以重试请求。我想,需要处理方法中的错误,.doOnError()或者.onErrorResumeNext()我被困在这里......

4

1 回答 1

2

单击对话框按钮时,您可以传递要执行的函数:

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)
  }
}
于 2018-03-28T20:15:59.013 回答