android - 可以在 Application 类中调用 Web 服务吗?
问题描述
我需要一个网络服务来持续跨活动。
所以作为一个场景想象ActivityOne
,ActivityTwo
和ActivityThree
。
ActivityOne
开始,然后单击一个按钮并ActivityTwo
启动。现在ActivityOne
在后台。
ActivityTwo
调用 web 服务,但在 web 服务返回之前,ActivityTwo
被用户关闭(称为activity.finish()
)并ActivityThree
启动。
我希望来自被调用的 Web 服务的 Web 服务响应ActivityTwo
被ActivityThree
.
基本上,无论活动如何,我都需要来自网络服务的响应。
我正在考虑在Application
课堂内调用网络服务。这是一个好主意吗?
解决方案
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).
推荐阅读
- css - 在芯片自动完成组件的输入字段之外显示选定选项的值
- linux - 使用 sed 命令在 linux 中编辑配置文件
- java - 我无法使用 Gradle、Dropwizard 和 IntelliJ 调试 JavaEE 应用程序
- reactjs - mauron85 background-geolocation 安装问题(react-native: command not found)
- java - 升级 JRE j2se 1.8.0_181 后无法启动 Java Web Start
- java - 在 weblogic 服务器上部署 Spring Boot Web 应用程序后,得到错误为 ORA-01427:单行子查询返回多于一行
- postgresql - 如何在 PL/pgSQL 函数中获取执行的客户端详细信息
- c# - C#图像丢失YCbCr(CMYK)和文件大小增加五倍
- java - Spring Boot - 配置上下文路径
- javascript - How to make form submit in bootstrap 4