取消的 AsyncTask 挂起新的 HttpRequest
我遇到了 asynctasks 和 http 请求的问题。
用户可以按下两个不同的按钮。这两个按钮都从网络服务器下载信息。我希望这些请求可以取消。每次调用之前都会显示一个进度对话框。
http请求是在异步任务中执行的,并且我在进度对话框上有一个 OnDismiss 监听器,如果按下后退键,它会取消异步任务。
效果是 50/50。
如果我执行http请求,取消asynctask,然后执行一个新请求,它可能会起作用,但也可能导致http请求无限挂起。
这行是它挂起的地方:
response = httpclient.execute(urlConnection, context);
是否有其他方法可以执行可取消的调用,或者我可以采取什么措施来避免挂起?
I'm having problem with asynctasks and http requests.
The user can press two different buttons. Both buttons download information from a web server. I want the requests to be cancelable. Before each call a progressdialog is shown.
The http request is performed in an asynctask, and I have an OnDismiss listener on the progressdialog that cancels the asynctask if back is pressed.
It works 50/50.
If I do the http request, cancel the asynctask, then do a new request, it might work but it might also cause the http request to hang infinite.
This line is where it hangs:
response = httpclient.execute(urlConnection, context);
Is there any other way to do a call that is cancelable or is there anything I could do to avoid the hang?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我编写了一个
AsyncTask
来通过 http 执行一些下载,我发现在任务上调用cancel()
实际上并不会停止任务的执行。 “取消”的任务将继续执行其后台操作。调用cancel()
似乎只是调用了onCancelled()
,然后AsyncTask
不会 调用onPostExecute() 完成后。也许您的第一个任务仍然拥有下一个任务所需的某些网络资源?
I wrote an
AsyncTask
to perform some downloads over http, and I found that callingcancel()
on the task doesn't actually stop the task from executing. The "cancelled" task will continue to perform its background actions. Callingcancel()
just seemed to callonCancelled()
and then theAsyncTask
doesn't callonPostExecute()
when it has finished. Perhaps your first task still has hold of some network resource that the next task requires?尝试打电话
Try calling