首页 > 解决方案 > 可以在 Application 类中调用 Web 服务吗?

问题描述

我需要一个网络服务来持续跨活动。

所以作为一个场景想象ActivityOneActivityTwoActivityThree

ActivityOne开始,然后单击一个按钮并ActivityTwo启动。现在ActivityOne在后台。

ActivityTwo调用 web 服务,但在 web 服务返回之前,ActivityTwo被用户关闭(称为activity.finish())并ActivityThree启动。

我希望来自被调用的 Web 服务的 Web 服务响应ActivityTwoActivityThree.

基本上,无论活动如何,我都需要来自网络服务的响应。

我正在考虑在Application课堂内调用网络服务。这是一个好主意吗?

标签: android

解决方案


You can make a singleton class for your webservice call and store webservice response in that class. next time in your ActivityThree when you call webservice, you can check if response is already there then return response or you can make a call. it will remove your data lost in case of broadcast or event bus (like when you broadcast your response using broadcast receiver or event bus and in case your receiver is not registered then you will not receive your response).


推荐阅读