gpt4 book ai didi

android - 再次开始 Retrofit 调用,从 RXJava 中的 onError 回调再次订阅 Observable

转载 作者:行者123 更新时间:2023-11-29 23:38:07 26 4
gpt4 key购买 nike

当 Retrofit Call 不成功时(例如因为没有互联网),将按预期调用 RXJava 回调 onError,其中我有一个 Snackbar 和一个 setAction() 监听器用一个直观的字符串“重试”指示我应该在其中传递命令以重新开始网络调用。我可以重新启动我在内部调用它的类 NameActivity(),但这看起来很糟糕。我可以在 Snackbar 监听器中传递哪个命令来重新启动下面的代码?

 MyViewModel!!.getPost("132")
?.subscribeOn(schedulerProvider!!.io())
?.observeOn(schedulerProvider!!.ui())
?.doOnNext {
run {
spinner.setVisibility(View.VISIBLE)
}
}

?.subscribe(object : FlowableSubscriber<List<Post>> {
override fun onError(t: Throwable?) {
spinner.setVisibility(View.GONE)

spinner.visibility
Snackbar.make(view.findViewById(R.id.linearLayout), "Check Internet Connection!", Snackbar.LENGTH_INDEFINITE)
.setAction("Retry", {})//HERE THE COMMAND SHOULD PASS
.show();

}

override fun onComplete() {
Log.d("TAG", "onComplete: ")
}

override fun onSubscribe(s: Subscription) {
s.request(Long.MAX_VALUE);

}

override fun onNext(posts: List<Post>?) {
spinner.setVisibility(View.GONE)

posts?.let { viewAdapter.setTitleData(it) }

}
})
}

最佳答案

如果你想在调用失败时立即显示 snackbar,你必须再次调用订阅。您可以按如下方式重构代码:

  1. 将您的 Rx 代码放在一个方法中。例如doNetworkCall()
  2. 创建一个单独的方法来处理您的错误。例如handleError(t: Throwable)
  3. 在该方法中,您可以让您的 Snackbar 出现,并在用户点击重试操作后调用 doNetworkCall() 方法。

例子:

fun doNetworkCall() {
MyViewModel!!.getPost("132")
?.subscribeOn(schedulerProvider!!.io())
?.observeOn(schedulerProvider!!.ui())
// The rest of the code here was removed for brevity.
?.subscribe(object : FlowableSubscriber<List<Post>> {
override fun onError(t: Throwable?) {
spinner.setVisibility(View.GONE)
spinner.visibility

handleError(t) // Call the method here.
}
// The rest of the code here was removed for brevity.
}

fun handleError(t: Throwable?) {
// Here you can also check the type of the error, and perhaps even change the snackbar action depending on it.
Snackbar.make(view.findViewById(R.id.linearLayout), "Check Internet Connection!", Snackbar.LENGTH_INDEFINITE)
.setAction("Retry", doNetworkCall()) // Set the retry action to call the doNetworkCall() method again.
.show()
}

您还可以在向用户提示任何内容之前使用自动重试。这可以通过使用 Rx retry operator 来实现.如果您的网络调用失败,它会自动重新订阅给定的尝试次数。

如 ReactiveX 文档所述:

  • One variant of retry takes no parameters. It will continue to resubscribe to and mirror the source Observable no matter how many onError notifications it receives.

  • Another variant of retry takes a single parameter: a count of the number of times it should try to resubscribe to the source Observable when it encounters errors. If this count is exceeded, retry will not attempt to resubscribe again and will instead pass the latest onError notification to its observers.

  • A third variant of retry takes a predicate function as a parameter. You write this function to accept two arguments: an Integer count of how many retries have taken place thusfar, and a Throwable indicating the error that caused the onError notification. This function returns a Boolean to indicate whether or not retry should resubscribe to and mirror the source Observable. If it does not, then retry passes the latest onError notification to its observers.

关于android - 再次开始 Retrofit 调用,从 RXJava 中的 onError 回调再次订阅 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52082763/

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