简体   繁体   English

Android-如何处理AsyncTask超时?

[英]Android - How to handle AsyncTask timeouts?

In my app, I use AsyncTask classes to make HTTP connections and download information to display. 在我的应用程序中,我使用AsyncTask类建立HTTP连接并下载要显示的信息。

Sometimes, the amount of data is substantial and takes some time. 有时,数据量很大,需要一些时间。 If a user and their phone happens to be in a place with slow/poor/spotty reception, there might be HTTP timeouts or maybe they loose connectivity all together, etc... 如果用户和他们的手机恰好在接收/接收速度慢/差的地方,则可能是HTTP超时,或者他们可能完全失去连接,等等。

What is the best way to handle these timeouts gracefully? 妥善处理这些超时的最佳方法是什么?

I've been expirimenting with creating a Runnable handler that runs XX seconds after starting the AsyncTask . 我一直很想创建一个Runnable处理程序,该处理程序在启动AsyncTask之后运行XX秒。 The Runnable would simply test to see if the AsyncTask is still running and if so, assume the data connection is too slow and go ahead and kill the thread and gracefully give an error message about the data connection. Runnable会简单地进行测试以查看AsyncTask是否仍在运行,如果是,则假定数据连接速度太慢,然后继续杀死该线程并优雅地给出有关数据连接的错误消息。

Is this a good approach or is there a more practical method? 这是一个好方法还是更实用的方法?

It's been a while since I've programmed for Android, but I know that AsyncTask has an update event (to track download percentages and such). 自从我为Android编程以来已经有一段时间了,但是我知道AsyncTask有一个更新事件(用于跟踪下载百分比等)。 You could probably use that to have it check itself. 您可能会使用它来对其进行检查。

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

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