android - 我如何在 android 应用程序中模拟 REST 后端
问题描述
我的 android 应用程序通过 REST API 与后端服务通信。我想模拟出这个 API 来快速开发前端。我正在使用 android volley 作为客户端网络库..
解决方案
我会将模拟数据作为 json 存储在我的资产文件夹中,并从这些 json 文件创建我的数据:
fun getJsonFromAssets(context: Context, jsonPath: String): String? {
return try {
context.assets.open(jsonPath).bufferedReader().use{
it.readText()
}
} catch (e: IOException) {
null
}
}
然后得到像这样的对象(这里以列表为例):
val list = gson.fromJson<List<MyObject>>(jsonString, object : TypeToken<List<MyObject>>() {}.type)
推荐阅读
- azure-devops - 如何修复从 Azure 数据工厂 V2 到 AzureDevops 的 REST 连接中的“无法从 Rest Source 读取”错误?
- javascript - 带有范围滑块 JS 的马来西亚等值线图
- ios - In-Memory Realm 是否有 Swift 中磁盘持久化 Realm 数据的副本?
- java - Java缓存SSL失败 - 我可以以某种方式刷新这些
- django - 从对象列表中接收相关模型的列表(由外键绑定)。姜戈
- virtualenv - 如何在 Raspberry Pi3 上的 Python3 虚拟环境中安装 PySide2
- java - LibGDX:最大屏幕宽度
- angular - 在spring应用程序中集成angular app,JSP页面[TypeError]
- dataset - 卡在 ckan 中的数据集无法删除,无法打开 (404)
- python - 将 WindowsPath 转换为 PosixPath